Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 861419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:58:36+00:00 2026-05-15T08:58:36+00:00

Small example: perl -e ‘$s=aaabbcc;$c=()=$s=~/a/g;print$c\n$s\n’ (m//g) outputs 3 aaabbcc whereas perl -e ‘$s=aaabbcc;$c=()=$s=~s/a/x/g;print$c\n$s\n’ (s///g)

  • 0

Small example:

perl -e '$s="aaabbcc";$c=()=$s=~/a/g;print"$c\n$s\n"' (m//g) outputs

3
aaabbcc

whereas perl -e '$s="aaabbcc";$c=()=$s=~s/a/x/g;print"$c\n$s\n"' (s///g) outputs

1
xxxbbcc

I’d like to do both things at once without having to match first: substitute and know the number of substitutions. Obviously a s///g does not return the number of substitutions in scalar context–unlike m//g does with matches. Is this possible? If yes, how?

perlre, perlvar and perlop provided no help (or I just couldn’t find it).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:58:36+00:00Added an answer on May 15, 2026 at 8:58 am

    s/// does return the number of substitutions made in scalar context. From perlop (emphasis added):

    s/PATTERN/REPLACEMENT/msixpogce
    Searches a string for a pattern, and if found, replaces that
    pattern with the replacement text and returns the number of
    substitutions made
    . Otherwise it returns false (specifically,
    the empty string).

    Your problem is that you didn’t call s/// in scalar context. You called it in list context and then evaluated the assignment (to an empty list) in scalar context. A list assignment in scalar context returns the number of elements produced by the right-hand side of the expression. Since s/// returns a single value (in both list and scalar context) the number of elements is always one even if the s/// didn’t do anything.

    perl -E "$s='aaabbcc'; $c=()=$s=~s/x/y/g; say qq'$c-$s'"  # prints "1-aaabbcc"
    

    To call s/// in scalar context, omit the =()= pseudo-operator.

    perl -E "$s='aaabbcc'; $c=$s=~s/a/x/g; say qq'$c-$s'"  # prints "3-xxxbbcc"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a small Example of what I would like to achieve: Maven Artifact
Here is small example: A <- c(1,1,1,1, 0, 0, 0, 2,2,2) B <- c(1,1,1,1,
I have a small example Sencha Touch 2 app at http://www.senchafiddle.com/#FWMDh . It populates
After using grep command, i will give you a small example grep test test
I need some help adding conditions to a linq query. Take this small example
I have big dataset (but the following is small one for example). I can
For Example: I need to develop a Small Inventory program for nearby Super Market
for example: I have a string in the resources: <string name="smth"><small>hey girls</small></string> When I
I'm creating simple example on JSF. It's small Login application. My problem is duplicate
i'm just a begginer in perl, and very urgently need to prepare a small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.