|Do you know where your variables are?|
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||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.
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
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
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 ;-)