Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by Laurent_R (Parson)
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)
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-09-23 08:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (211 votes), past polls