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

Hello all, I am trying to load the output of a command into an array. The command output is

Job Name Last Start Last End ST Run + Pri/Xit ____________________________ ________________ ________________ __ ____ +___ ___ OPR_D_FTP_DAILY_NY_NT 03/13/2003 06:30 03/13/2003 06:30 SU 7252 +38/1

Using this code (example 1) I can load the entire output into the array, but when I try to grep (example 2) to just load the data, not the header I have problems. Example 1

my $job_name="OPR_D_FTP_DAILY_CLT_NT"; my @job_autorep= split /\s+/,`autorep -j $job_name`;

Example 2

my @job_autorep= split /\s+/, grep {/$job_name/},`autorep -j $job_name`;

Example 2 returns 1 as the value. I have tried changing the split pattern without any luck, also just using grep without the split returns the data line that I want, but loads it as one value in the array. Any suggestions? Thanks.

Replies are listed 'Best First'.
Re: Split into an array
by Thelonius (Priest) on Mar 13, 2003 at 22:41 UTC
    pfaut is on the right track, but the grep won't work like you want. Perl's grep takes a list, so the grep is going to return the whole multi-line string output. You can either (untested code ahead):
    my @job_autorep= split /\s+/, `autorep -j $job_name | grep "$job_nam +e"`;
    or
    my @job_autorep = map { split /\s+/ } grep /$jobname/, split /\n/, ` +autorep -j $job_name`;
Re: Split into an array
by pfaut (Priest) on Mar 13, 2003 at 21:58 UTC

    split wants a scalar but grep provides a list. If you know you're only going to get one matching line back, try this.

    my @job_autorep= split /\s+/, (grep {/$job_name/},`autorep -j $job_nam +e`)[0];
    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: Split into an array
by dga (Hermit) on Mar 13, 2003 at 22:49 UTC

    I am not exactly sure what you are trying to do but some part of this should do at least part of it. Assuming the job you want is always the 3rd line of output.

    my $arh; my $job; open($arh, "autorep -j $job_name |"); ($job)=(<$arh>)[2]; chop $job; my @job_autorep=split /\s+/, $job;

    When I declared my variables $arh and $job on the lines where they first get values assigned, they were left undefined. Moving the declarations to above the open works as I expected. $job gets the third line of output (after the 2 header lines). Then split that and you are set. Be aware that the start and end will be 2 fields each, a date and a time. The parens around $job force array context which is needed to get the 3rd line directly from the output. Also the entire output will be scanned even though all lines other than the 3rd will be ignored. From your post it looks like there will always be exactly 3 lines returned from autorep. I would also strongly recommend that you use the full pathname for autorep or at least set the $ENV{PATH} variable to an explicitly safe value to make sure this perl program gets the data from the correct source.

Re: Split into an array
by cutter (Acolyte) on Mar 14, 2003 at 14:53 UTC
    Thanks all. I had looked at both solutions, using the unix grep or just selecting the 3rd line, but I wanted to understand why the perl grep was not working as I expected.