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

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" ;

Replies are listed 'Best First'.
Re^5: Store the outcome of a foreach loop in an array
by haukex (Archbishop) on Jan 11, 2019 at 08:05 UTC

    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!

Re^5: Store the outcome of a foreach loop in an array
by AnomalousMonk (Archbishop) on Jan 10, 2019 at 23:39 UTC
    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:  <%-{-{-{-<

Re^5: Store the outcome of a foreach loop in an array
by LanX (Saint) on Jan 11, 2019 at 00:04 UTC
    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