http://www.perlmonks.org?node_id=1055792


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.