Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Regular Expression: search two times for the same number of any signs (updated)

by Anonymous Monk
on Nov 29, 2016 at 12:41 UTC ( [id://1176799]=note: print w/replies, xml ) Need Help??


in reply to Re: Regular Expression: search two times for the same number of any signs (updated)
in thread Regular Expression: search two times for the same number of any signs

Hi Hauke,

I am trying to fully understand all the new things. One question to your construct "for"

print "$_ =>\n" for qw/ 1 12 123/;

is working fine. I like this style. But I can not combine it with an if or multiple lines.

{print "$_ =>\n" if $_=/1/ } for qw/ 1 12 123/;

gives me floowing error message: "Missing $ on loop variable at ./test3.pl line 2." and I can not understand, what is mean by this error meassage.

Replies are listed 'Best First'.
Re^3: Regular Expression: search two times for the same number of any signs (updated)
by haukex (Archbishop) on Nov 29, 2016 at 13:19 UTC

    Hi Anonymous,

    You can't use more than one statement modifier like for or if at a time (and I think that if you were able to, it would lead to more hard-to-understand code). If your code gets more complex you should instead use a normal for loop:

    for my $n (qw/1 12 123 234/) { print "$n =>\n" if $n=~/1/; }

    (Ok, there is a way to do what you want, but legibility begins to suffer if it gets longer: /1/ and print "$_ =>\n" for qw/1 12 123 234/;)

    Update: I should add that I was golfing a little bit in my example code, and that compressed style is not necessarily something one should strive to use in production code ;-)

    Regarding the other question about (??{ }), that's documented along with (?{ }) in perlre. The oversimplified explanation is that the code inside (??{...}) is evaluated and its return value embedded as part of the regular expression (but make sure to read the docs). So in my regex, the code '.{'.length($2).'}' takes the length of the string matched in between the first set of x's (x(.*)x), and then generates an expression like .{N} (where N is the length), so if the input were x12345x67890x, the regular expression it is matched against is x.*x.{5}x.

    Hope this helps,
    -- Hauke D

    Updated wordings a little bit.

      Hi Hauke,

      Many many thanks for your explainations and tips !!!!!

      Took a while to understand, read and try out ... I really learned thinks , that are a complet new level of perl for me !

      Thanks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-24 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found