Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by perlynewby (Beadle)
on Oct 16, 2020 at 16:49 UTC ( #11122913=perlquestion: print w/replies, xml ) Need Help??

perlynewby has asked for the wisdom of the Perl Monks concerning the following question:

I am getting an unexpected output from a one-liner and I do not understand what it is I am doing wrong. the one-liner is outputting a "1" before the line number. I've tried to simplify the one-liner to try to understand the output but still eludes me but, maybe, someone here can part with some of the wisdom.

____data.txt____

name = Leah last_name = Pellegrini age = 32 profession = "artist" / "e +ngineer of all things" favorite_song = "I'm alive" performer = "Micha +el Franti" name = alberto last_name = Montelongo age = 32 profession = "cool + dude" favorite_song = "#41" performeer = "DMB" Name = Lucca last_name = montelongo age = 6 profession = "future Blue +Angel" favorite-song = "sunflower" performer = "Post Malone" Name = josh last_name = montelongo age = 26 profess +ion = "happy" favorite-song = "satellite" performer = "dmb" Name = josh last_name = montelongo age = 26 profession = "happy" favo +rite-song = "satellite" performer = "dmb"

1) One-liner seeking to build up to do multiple things like only print odd lines, print lines with text only, OR print if regexp match. Then print line with no leading space, number of the lines, space out with a \tab.

 perl -lane 'print s/^\s+//,$., "\t", @F[0,1,2,],"\t",@F[3,4,5], if (($.%2) && /\w+/ || /cool dude/)' data.txt
13 name=alberto last_name=Monte 5 Name=Lucca last_name=monte

Please, notice the 1 before the odd line number 3, Why is that? Is my substitution not written correctly? I am replacing the space with no space. If I remove this substitution the output is fine but it is unexpected to me and I cannot understand the mechanism that is at work here with this substitution.

2) Now, the same one-liner but printing the even lines.

perl -lane 'print s/^\s+//,$., "\t", @F[0,1,2,],"\t",@F[3,4,5], if (($.%2==0) && /\w+/ || /cool dude/)' data.txt
2 name=Marta last_name=Pellegrini 13 name=alberto last_name=Monte 16 Name=josh last_name=monte 8 Name=josh last_name=monte

This is to illustrate the susbtitution in line 3 (satisfies OR condition) and 6(even lines). I'll appreciate and explanation why the substitution is outputing these ones.

Additional challenge: count the repeating names in the data.txt. I did a ~tr/=// as a character count but unable to think of a counter for "name" to add to this one-liner ;-)

  • Comment on One liner is returning unexpected "1" on the output and I do not understand the mechanism at play.
  • Select or Download Code

Replies are listed 'Best First'.
Re: One liner is returning unexpected "1" on the output and I do not understand the mechanism at play.
by choroba (Archbishop) on Oct 16, 2020 at 16:55 UTC
    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]

      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.

      Run the substitution before printing the result.

      ...or use s///r

        ...or use s///r

        not what OP wants

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11122913]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2020-10-31 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (287 votes). Check out past polls.

    Notices?