in reply to Perl: Split/format the output and append it to an array

0) Zero first.
I have the below code

What is my $stream = streamname; meant to do? "streamname" is a bare word, and without strict it will be interpreted as a literal string. What for?

my $xlink,@xlinkpaths;

This doesn't do what you want. To give my a list, you have to wrap the list in parens:

perl -Mstrict -ce 'my $xlink,@xlinkpaths' Global symbol "@xlinkpaths" requires explicit package name at -e line +1. -e had compilation errors. perl -Mstrict -ce 'my($xlink,@xlinkpaths)' -e syntax OK

but in

my @xlinks = (`cmd to get xlinks`) ;

the parens are gratuitous.

foreach $xlink (@xlinks) { @xlinkpaths = }

Missing right hand side of assignment. Ok, you ask for that in

1)

First, you don't want to assign to @xlinkpaths, but append to it. Use push. Then, to get the text after /./, you could use a regular expression:

foreach $xlink (@xlinks) { push @xlinkpaths, $xlink =~ m{incl -s streamname /./(.+)}; }

Look up the match operator m// (used as m{} here) in perlop.
The match operator returns 1 upon success; if used in list context (this is the case with push) it returns 1, if no captures are used (a capture is what is caught by parens, in the above code (.+), otherwise it returns the captures.
Captures are stored in $1, $2, $3, ...

The above code could have been written as

foreach $xlink (@xlinks) { $xlink =~ m{incl -s streamname /./(.+)}; push @xlinkpaths, $1; }

which gives you the possibility to not include empty matches via a statement modifier:

push @xlinkpaths, $1 if $1;

2)

As you have already learned by now, use push to append to an existing array. Use unshift to prepend to an existing array. Your final @xlinkpaths looks like prepending.

unshift @exclpaths, @xlinkpaths;

Your code - after cleanup and inserting mising bits - could look like

use strict; use warnings; my( $xlink,@xlinkpaths ); my $stream = streamname; my @xlinks = (`cmd to get xlinks`) ; foreach $xlink (@xlinks) { push @xlinkpaths, $xlink =~ m{incl -s streamname /./(.+)}; } my @exclpaths = ( "wefwfewf/fewfff/", "btrtbrtb/gbrgbrg/rttty/", ); unshift @exclpaths, @xlinkpaths;

and appears to do what you want.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^2: Perl: Split/format the output and append it to an array
by sravs448 (Acolyte) on Sep 12, 2014 at 15:03 UTC
    The first part worked fine. But before prepend(or)after the array, i would like to have the @xlinkpaths contents also in the same pattern as @exclpaths. That is
    1.every value should end with '/'
    2. should be enclosed within quotes ""
    3. Should have comma ',' at end
    How can I achieve this? I want to maintain the same exisitng format in @exclpaths array
      1.every value should end with '/'
      2. should be enclosed within quotes ""
      3. Should have comma ',' at end

      See the concatenation operator '.' in perlop, Additive Operators, and substr.

      Read perlop. All of it.

      I want to maintain the same exisitng format in @exclpaths array

      The format in your @exclpaths is its representation in code. Inside the array, the strings are not enclosed within double quotes, and they don't have a comma at the end:

      my @exclpaths = ( "wefwfewf/fewfff/", "btrtbrtb/gbrgbrg/rttty/", ); print $_,"\n" for @exclpaths; __END__ wefwfewf/fewfff/ btrtbrtb/gbrgbrg/rttty/

      If you want decorations, just add them:

      my @exclpaths = ( "wefwfewf/fewfff/", "btrtbrtb/gbrgbrg/rttty/", ); print "\"$_\",\n" for @exclpaths; __END__ "wefwfewf/fewfff/", "btrtbrtb/gbrgbrg/rttty/",
      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'