Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Welcome to the Monastery
 
PerlMonks  

Re: Substituting white spaces with a tab changes my string into numeric value

by dsheroh (Parson)
on Sep 20, 2012 at 08:17 UTC ( #994608=note: print w/ replies, xml ) Need Help??


in reply to Substituting white spaces with a tab changes my string into numeric value

To explain what was going on here:

s/\s+/\t/g; is equivalent to $_ =~ s/\s+/\t/g;, so $line =~ s/\s+/\t/g; actually means $line = ($_ =~ s/\s+/\t/g); (The parentheses aren't actually necessary, but I wanted to make the precedence completely clear.)

In scalar context, a regex match or substitution (=~) returns the number of matches found/substitutions performed, so, to finally get to where that 2 comes from, $line =~ s/\s+/\t/g; changes all the groups of whitespace in $_ into tabs and sets $line to the number of substitutions which were performed. If you look at $_, you will find that it holds the value you wanted, since $line and $_ held the same value before the substitution was performed.


Comment on Re: Substituting white spaces with a tab changes my string into numeric value
Select or Download Code
Re^2: Substituting white spaces with a tab changes my string into numeric value
by PerlPeer (Novice) on Sep 20, 2012 at 09:22 UTC

    Thank you too.

    I figured that somehow I got a numerical value of the number of tabs, since when I used:

     s/\s+/\t/

    (thus without the "g") the number that was returned was 1 (instead of 2). I had no idea however, how this was caused.

    I frequently fail to recognize this kind of "equivalents". I hope this is just being new to perl.......

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://994608]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-04-20 00:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls