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

justsomeguy has asked for the wisdom of the Perl Monks concerning the following question:

I'm a recovering IT manager looking for the most efficient way of assigning the output of this print statement to an array, which will later be written through DBI to an SQLDB. Right now, just need to build an array off of it:

while (<FILE>) { ( print $_ if (/Run as/ .. /^ /); }

I know I did something similar, long ago. Hoping one of you kind folks will help to jog my memory and shake loose my dormant perl.

Thank you.

Replies are listed 'Best First'.
Re: Assign Output of Regex to Array (newbie)
by davido (Cardinal) on Sep 25, 2013 at 19:16 UTC

    Are you looking for push?

    while( <FILE> ) { push @array, $_ if /Run as/ .. /^ /; }

    Dave

      Well, that's a start. Do I need a chomp and split as well? The data is space-delimited.

        If I were dealing with a space delimited file of simple characteristics, and I wanted to insert it into a multidimensional array, along with a /start/ .. /end/ conditional, I might do it like this:

        while( <$fh> ) { chomp; next unless length; if( /start/ .. /end/ ) { push @array, [ split /\s+/, $_ ]; } }

        Your definition of the start and end pattern are difficult for me to guess without knowing more about the input data, but you were probably on the right track to begin with there.

        If there's any risk that the input file could contain things like spaces embedded within quoted fields, or other more complex constructs that are challenging for split, then you would want to shift to using Text::CSV, specifying space as the delimiter rather than commas.

        Oh, one last thought; if the input data has fixed-width fields, then splitting on space becomes less important; you could just unpack based on a fixed unpack specification.


        Dave

Re: Assign Output of Regex to Array (newbie)
by NetWallah (Canon) on Sep 25, 2013 at 19:32 UTC
    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.

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


        Dave

        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.

Re: Assign Output of Regex to Array (newbie)
by Anonymous Monk on Sep 26, 2013 at 00:36 UTC
    See "Arrays" section in Learn Perl in about 2 hours 30 minutes

    Or see "Array Operations" or "Regular Expressions and Matching / Regex Modifiers" in the free book Modern Perl a loose description of how experienced and effective Perl 5 programmers work....You can learn this too.