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

Re: Need advice for perl use as awk replacement

by hippo (Chancellor)
on Sep 21, 2020 at 17:33 UTC ( #11122014=note: print w/replies, xml ) Need Help??


in reply to Need advice in for perl use as awk replacement

Each successful match sets the numbered capture groups so you are correct in thinking that the second match you perform clobbers the $3 from the first. You are also correct in that storing the capture groups from the first match in some other structure would help. Where you have gone wrong is that you are storing references (see perlreftut) to the capture groups so you are back at square one. Instead you should store the values, eg:

#!/usr/bin/env perl use strict; use warnings; my $string = 'foo bar baz quux'; $string =~ /(\w+) (\w+) (\w+) (\w+)/; print "Numbered groups: $1 $2 $3 $4\n"; my @matches = ($1, $2, $3, $4); print "\@matches has @matches\n"; $3 =~ /^baz/; print "Numbered groups after match 2: $1 $2 $3 $4\n"; print "\@matches after match 2: @matches\n";

🦛

Replies are listed 'Best First'.
Re^2: Need advice for perl use as awk replacement
by Anonymous Monk on Sep 21, 2020 at 21:54 UTC

    Thanks, me storing references is very interesting. At first I did not see exectely where your code does difeer to mine in that point, simply because I did not know the perl reference/value syntax. I was expecting a python like approach, but was wrong.

    The interesting thing is that my script has \$1 .... which if I understand correctly would be references for python, but actually I user double quotes in th ebash script which requires me to pu \$1 to avoid $1 to be replaced by the shell. This I expect perl to see "$1,$2,$3..." without backslash, eg. value. Am I wrong here ?

    The reason I use double quotes is that later thea ctual additional filtering conditions will be injected via shell variable depending on the bash function call arguments.

    But I realize that actually my variable name for th earray "$m" was wrong and should be "@m". Cannot try now, but will give it a try tomorrow.

      actually I user double quotes in th ebash script which requires me to pu \$1 to avoid $1 to be replaced by the shell

      Ah yes, you are quite right. I have to agree with brother jcb that using a one-liner for this, especially with double quotes, is just asking for trouble and will be a maintenance nightmare. Just put it all in a script and then you won't have to worry about escaping everything. Eventually, when you become more familiar with Perl and what it can do you may see the benefit in replacing all of the bash parts with Perl.

      There's a place for one-liners but this doesn't look like that place to me.


      🦛

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2021-01-27 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?