in reply to Re: Store the outcome of a foreach loop in an array
in thread Store the outcome of a foreach loop in an array

If I print "@$lines", I get this:

c1r1,c2r1,c3r1,c4r1,c5r1 c1r2,c2r2,c3r2,c4r2,c5r2 c1r3,c2r3,c3r3,c4r3,c5r3
I was trying to store "c3r1 c3r2 c3r3" in "@store" - but it only saves "c3r1" and comes out of the loop :(

Replies are listed 'Best First'.
Re^3: Store the outcome of a foreach loop in an array
by haukex (Archbishop) on Jan 10, 2019 at 22:47 UTC
    but it only saves "c3r1" and comes out of the loop

    This would happen if @$lines only has a single element, "c1r1,c2r1,c3r1,c4r1,c5r1\nc1r2,c2r2,c3r2,c4r2,c5r2\nc1r3,c2r3,c3r3,c4r3,c5r3". As I showed here, please use Data::Dumper to print out $lines, as in: "use Data::Dumper; $Data::Dumper::Useqq=1; print Dumper($lines);". If that shows a single string with \n's in it, then my assumption is correct. Instead of doing something like using split on that string, it's probably better if we took a look at where you're getting $lines from - please have a look at Short, Self-Contained, Correct Example, a SSCCE would be useful so we can run the code for ourselves and see exactly the issue you're having.

      Here is SSCCE

      #!/usr/bin/perl use customlib; my $file = 'cr.csv'; my $lines; rdfile($file,\$lines); # Remove comments and blank lines @$lines = grep(!/^\s*#/,@$lines); @$lines = grep(!/^\s*$/,@$lines); @$lines = join "\n", @$lines; my @store; foreach my $line (@$lines) { my @val = split(/\,/,$line); push (@store,$val[2]); } #printf "@store\n"; use Data::Dumper; $Data::Dumper::Useqq=1; print Dumper($lines);
      And you were right - @$lines with Data::Dumper printed the following output: $VAR1 = "c1r1,c2r1,c3r1,c4r1,c5r1\nc1r2,c2r2,c3r2,c4r2,c5r2\nc1r3,c2r3,c3r3,c4r3,c5r3" ;

        In addition to the comments from the others:

        @$lines = join "\n", @$lines;

        Basically, this is your issue - you're joining the lines into a single string, which is why your loop runs only once. This line probably isn't necessary - unless you had a specific reason for putting it there? It'd be best if you insert more debugging output - for example, put the use Data::Dumper; $Data::Dumper::Useqq=1; at the top of the program, and then do a print Dumper($lines); before the join, and maybe once more right after the rdfile, to see how the array changes as your program runs.

        BTW, it's best if you Use strict and warnings!

        use customlib;
        my $file = 'cr.csv';

        Side Note: Please understand that an SSCCE that depends on an unspecified custom module and unspecified data is not "self-contained."

        But you appear to have resolved your issue, so we're home and dry.


        Give a man a fish:  <%-{-{-{-<

        I think either your rdfile() doesn't do what you think it should or your CSV holds all data in one line.

        I could show you how to split on "\n" into multiple lines but that's rather dangerous with CSV input which could have line breaks inside content.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        </div
Re^3: Store the outcome of a foreach loop in an array
by LanX (Saint) on Jan 10, 2019 at 22:46 UTC
    Are you sure you showed us all the code?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice