Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by dsheroh (Monsignor)
on Sep 20, 2012 at 08:17 UTC ( [id://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.

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://994608]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-26 03:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found