Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
"be consistent"
 
PerlMonks  

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

by justsomeguy (Novice)
on Sep 25, 2013 at 20:04 UTC ( #1055742=note: print w/ replies, xml ) Need Help??


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

I ended up doing something similar to that. Thanks!

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

Now I need to add an element to each line in the array. Is that where push comes in?


Comment on Re^2: Assign Output of Regex to Array (newbie)
Download Code
Re^3: Assign Output of Regex to Array (newbie)
by Kenosis (Priest) on Sep 25, 2013 at 21:03 UTC

    You can use the following notation to do that:

    $_ .= ' an element' for @PRC_PRIV_ONLY;

    This iterates through the array, appending the string ' an element' to each array element.

Re^3: Assign Output of Regex to Array (newbie)
by davido (Archbishop) on Sep 25, 2013 at 22:06 UTC

    This approach gives you a flat array. If you want to add an element to each line, you have to decide if it's satisfactory to just append the information to existing elements, or whether you really need a multi-dimensional data structure. For example:

    @PRC_PRIV_ONLY = ( 'one string', 'two strings', 'three strings', ); foreach my $element ( @PRC_PRIV_ONLY ) { $element .= ' appended data'; }

    Or if you need a 2d data structure:

    foreach my $element ( @PRC_PRIV_ONLY ) { $element = [ $element, '2nd element' ]; }

    That transforms the above into:

    @PRC_PRIV_ONLY = ( [ 'one string', '2nd element' ], [ 'two strings', '2nd element' ], [ 'three strings', '2nd element' ], );

    ...which can be indexed like this: print $PRC_PRIV_ONLY[0][1], "\n"; (prints 2nd element).


    Dave

Re^3: Assign Output of Regex to Array (newbie)
by Laurent_R (Priest) on Sep 26, 2013 at 07:17 UTC

    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.

      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-04-17 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (444 votes), past polls