Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: One liner is returning unexpected "1" on the output and I do not understand the mechanism at play.

by choroba (Cardinal)
on Oct 16, 2020 at 16:55 UTC ( [id://11122914]=note: print w/replies, xml ) Need Help??


in reply to One liner is returning unexpected "1" on the output and I do not understand the mechanism at play.

As documented in perlop, the substitution

> Searches a string for a pattern, and if found, replaces that pattern with the replacement text and returns the number of substitutions made.

Your s/// replaced once, so it returned 1. Run the substitution before printing the result.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
  • Comment on Re: One liner is returning unexpected "1" on the output and I do not understand the mechanism at play.
  • Download Code

Replies are listed 'Best First'.
Re^2: One liner is returning unexpected "1" on the output and I do not understand the mechanism at play.
by perlynewby (Scribe) on Oct 16, 2020 at 19:25 UTC

    Yes, this is what I did not understand that replacing the space to no space would return 1. I went I did a few experiments to solidify my understanding. I spent so much time reading but could not understand the inner workings; I appreciate it so much.

Re^2: One liner is returning unexpected "1" on the output and I do not understand the mechanism at play.
by ikegami (Patriarch) on Oct 17, 2020 at 18:22 UTC

    Run the substitution before printing the result.

    ...or use s///r

      ...or use s///r

      not what OP wants

        Yes it is. They asked to "print line with no leading space" instead of "1".

        This can be achieved by replacing

        print s/^\s+//, ...; # This is printing 1
        with
        print s/^\s+//r, ...;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-18 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found