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

syntax question

by rruser (Acolyte)
on Oct 23, 2013 at 19:55 UTC ( #1059342=perlquestion: print w/ replies, xml ) Need Help??
rruser has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I exclude a field ending in X and I am not sure this is the best way to go about it. I want to make sure nothing falls thru the cracks.

if ($fld0 =~ (/X$/)) { next; }

thanks for any suggestions...

Comment on syntax question
Download Code
Replies are listed 'Best First'.
Re: syntax question
by toolic (Bishop) on Oct 23, 2013 at 20:19 UTC
    Keep in mind that the regular expression $ matches an optional \n, so these two will match:
    $fld0 = "abcX"; $fld0 = "abcX\n";

    Another possibility uses substr, which will not match the \n:

    if (substr($fld0, -1, 1) eq 'X') { next }
Re: syntax question
by keszler (Priest) on Oct 23, 2013 at 20:14 UTC
    Assuming that you're in some loop and the next command is how you "exclude a field ending in X", you have the right idea. The parentheses around the regular expression are not needed. I would usually write that as next if $fld0 =~ /X$/;, but if ($fld0 =~ /X$/) { next; } is effectively the same.

    Another option: next if substr($fld0,-1,1) eq "X"; (substr)

      Update: Sorry, I should have read the thread first, toolic said the same thing.

      next if substr($fld0,-1,1) eq "X";

      is not exactly equivalent to

      next if $fld0 =~ /X$/;

      since $ at the end of a regex is a bit special. From perlre #Metacharacters:

      $ Match the end of the line (or before newline at the end)

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

Re: syntax question
by rruser (Acolyte) on Oct 23, 2013 at 21:25 UTC

    thanks so much guys

Re: syntax question (some Bench)
by Discipulus (Priest) on Oct 24, 2013 at 07:25 UTC
    Consider also that regex are so optimezed so that the second option, using substr, 'may' become a 'little' faster then regexes only when you operate against huge data in a foreach loop:
    #10 fields in the array iterating over: Benchmark: running Regex, Substr for at least 4 CPU seconds... Regex: 4.58121 wallclock secs ( 4.18 usr + 0.02 sys = 4.20 CPU) + @ 3968640.46/s (n=16656384) Substr: 4.38291 wallclock secs ( 4.32 usr + 0.00 sys = 4.32 CPU) + @ 3927240.92/s (n=16969608) #100 fields Benchmark: running Regex, Substr for at least 4 CPU seconds... Regex: 4.56133 wallclock secs ( 4.12 usr + -0.01 sys = 4.11 CPU) + @ 3995786.36/s (n=16402703) Substr: 4.29298 wallclock secs ( 4.03 usr + 0.00 sys = 4.03 CPU) + @ 4190639.34/s (n=16871514) #1000 fields ###Regex is faster!! (tested 5 times) Benchmark: running Regex, Substr for at least 4 CPU seconds... Regex: 4.1849 wallclock secs ( 4.03 usr + 0.01 sys = 4.04 CPU) +@ 4139558.42/s (n=16723816) Substr: 5.5707 wallclock secs ( 4.26 usr + 0.00 sys = 4.26 CPU) +@ 3843370.27/s (n=16368914) #10000 fields Benchmark: running Regex, Substr for at least 4 CPU seconds... Regex: 4.87697 wallclock secs ( 4.68 usr + 0.00 sys = 4.68 CPU) + @ 3600221.37/s (n=16849036) Substr: 4.21117 wallclock secs ( 3.99 usr + 0.03 sys = 4.02 CPU) + @ 4352730.93/s (n=17519742) #BUT ALSO 10000 fields Benchmark: running Regex, Substr for at least 4 CPU seconds... Regex: 4.32358 wallclock secs ( 4.09 usr + -0.02 sys = 4.07 CPU) + @ 3946014.74/s (n=16064226) Substr: 4.44415 wallclock secs ( 4.10 usr + 0.03 sys = 4.13 CPU) + @ 3892862.84/s (n=16093095)
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (21)
As of 2015-07-30 20:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls