Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^7: Assign Output of Regex to Array (newbie)

by Laurent_R (Abbot)
on Oct 01, 2013 at 21:28 UTC ( #1056535=note: print w/replies, xml ) Need Help??

in reply to Re^6: Assign Output of Regex to Array (newbie)
in thread Assign Output of Regex to Array (newbie)

Just 1 ms? I thought it would be more. Against a total running time of how long?

I left off the "use warnings" and "use strict" as well as the variable declarations for this post in interest of brevity. But I always do both.

Fine, but then, you should rather declare your variables where you use them (in the smallest possible lexical scope), not at the top of your program. For example, you may have something like this:

foreach my $PRIV(@PRC_PRIV_ONLY) { chomp($PRIV); next if ($PRIV =~ m/Run|^ /); my ($USER, $HOSTN, $CMDN) = split(" ", $PRIV, 3); if (defined($USER)) { print "$PRC_GRP,$USER,$HOSTN,$CMDN \n"; } }

This way, you are defining truly local variables, and that's a good protection against silly but sometimes nasty bugs. Here above, $PRIV is scoped to the foreach loop, if you use it accidentally somewhere else in the program (which would have no sense), you'll get a compile error telling you that you are doing something wrong with it, and this is much better than having to chase the obscure reason why you don't get the output you expect. Similarly, the $USER, $HOST and $CMDN variables are scoped from the place they are declared to the end of the foreach loop. You'll be told by the compiler if you inadvertently use them where they should not be used.

Replies are listed 'Best First'.
Re^8: Assign Output of Regex to Array (newbie)
by justsomeguy (Novice) on Oct 23, 2013 at 20:02 UTC

    The file is only 50K in size. The 1ms improvement is about 15-20%, so scaled up that's considerable.

    Thanks again for the tips on variable scoping. My instinct is to put them all at top and declare globally, but the risks you cite are real.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1056535]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2017-09-26 22:23 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (298 votes). Check out past polls.