Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

by Laurent_R (Canon)
on Sep 26, 2013 at 07:17 UTC ( #1055792=note: print w/replies, xml ) Need Help??

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

Compared to the solution proposed by NetWallah, you are adding a temporary array and an additional processing step. This might have some impact on the memory footprint and performance of your program, but these things matter only if your input file is very large, not if you have a few hundreds or thousands lines.

  • Comment on Re^3: Assign Output of Regex to Array (newbie)

Replies are listed 'Best First'.
Re^4: Assign Output of Regex to Array (newbie)
by justsomeguy (Novice) on Oct 01, 2013 at 16:39 UTC

    Thanks for the helpful replies. I got it to work the way I need it to, and managed to address the warnings in regards to uninitialized variables and such. Here's the code for possible feedback:

    open(FILE,"<",$DATA_FILE); my @PRC_PRIV = <FILE>; my @PRC_PRIV_ONLY = grep /Run as/ .. /^ /, @PRC_PRIV; foreach $PRIV(@PRC_PRIV_ONLY) { chomp($PRIV); next if ($PRIV =~ m/Run|^ /); ($USER, $HOSTN, $CMDN) = split(" ", $PRIV, 3); if (defined($USER)) { print "$PRC_GRP,$USER,$HOSTN,$CMDN \n"; }

    Any feedback welcome.

      You said in the original post you wanted high performance. Try to replace this:

      my @PRC_PRIV = <FILE>; my @PRC_PRIV_ONLY = grep /Run as/ .. /^ /, @PRC_PRIV;

      with this:

      my @PRC_PRIV_ONLY = grep /Run as/ .. /^ /, <FILE>;

      And measure if you get any performance improvement.

      Another point is that you may want to include use strict; and use warnings; near the top of your program and declare your variables with the my operator. This is a recommended good practice which will save you a lot of debugging time.

        Thanks! I get about a 1ms improvement doing it that way, using "time" and after serveral iterations testing both ways. Small, but measurable.

        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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2017-11-19 04:59 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (278 votes). Check out past polls.