Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Assign Output of Regex to Array (newbie)

by NetWallah (Canon)
on Sep 25, 2013 at 19:32 UTC ( #1055738=note: print w/replies, xml ) Need Help??

in reply to Assign Output of Regex to Array (newbie)

How about:
my @array =grep {/Run as/ .. /^ /} <FILE>;

             My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Replies are listed 'Best First'.
Re^2: Assign Output of Regex to Array (newbie)
by justsomeguy (Novice) on Sep 25, 2013 at 20:04 UTC

    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?

      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.

      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).


      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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (13)
As of 2017-11-20 14:18 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (286 votes). Check out past polls.