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

Hi Monks!

I am trying to process the data for all the files I am reading from a directory, but I need to know how many fields are in each file before going into the foreach loop.
My goal is, if there are 4 values in the first row of the file file2.csv the push should be like:
push @all, [ $rows->[0], $rows->[1], $rows->[2], $rows->[3], ];
and so on for the rest of the files I am reading in depending on how many values are in each row of each file .
All the other rows in each file will have the same number of values as the first row.

I have this code where you can see what I am trying to do:
#!/usr/bin/perl use strict; use warnings; use Data::Dump 'pp'; use Data::Dumper; use Text::CSV; my $csv = Text::CSV->new ( { binary => 1, eol => "\n", quote_char => +'', } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); my @files = qw( file1.csv file2.csv file3.csv file4.csv file5.csv ); # I need to read all the files in @files once at a time my ($read_file, $name) = read_file(file => $files[0],); my ( $ready, $file_name ) = is_ready( do_data => $read_file, fname =>$ +name); print "$file_name:\n\n"; print Dumper $ready; sub read_file { my (%args) = @_; my $file = $args{ file }; my $fh; eval { open $fh, '<:encoding(utf8)', "dir/$file" or die "Could not open '$ +file' $!\n"; }; if ($@) { warn "Error opening $file"; } my @data; while (my $rows = $csv->getline( $fh )) { push @data, $rows; } if (not $csv->eof) { $csv->error_diag(); } close $fh; return \@data, $file; } sub is_ready { my (%args) = @_; my $data = $args{ do_data }; my $fname = $args{ fname }; my @all; # Need to process $data for all the files coming in, but # I have to know how many fields are in each file before going into # the foreach loop. # If there are 4 values in the file file2.csv the push should be like +: =code push @all, [ $rows->[0], $rows->[1], $rows->[2], $rows->[3], ]; =cut # and so on for the rest of the file. foreach my $rows ( @{ $data } ) { push @all, [ $rows->[0], $rows->[1], $rows->[2], ... ]; } my $send = \@all; return $send, $fname; } # =data file1.csv = 9 values in each row: eg. one,two,three,four,five,six,sev +en,eight,nine file2.csv = 4 values in each row eg. one,two,three,four file3.csv = 10 values in each row eg. one,two,three,four,five,six,seve +n,eight,nine,ten file4.csv = 7 values in each row eg. one,two,three,four,five,six,seve +n file5.csv = 5 values in each row eg. one,two,three,four,five
Thanks for helping out if possible!

Replies are listed 'Best First'.
Re: Pushing rows of data into array
by Marshall (Canon) on Apr 18, 2016 at 22:30 UTC
    I got a little lost in what you are trying to do. But looks like you are going to get a reference to an Array of Array. You don't want to "flatten" this out. It will be easier to process each row if you maintain this structure. See: http://perldoc.perl.org/5.8.8/perldsc.html#ARRAYS-OF-ARRAYS.

    Here is some example code:

    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @data = ([qw/one two three four five/], #array of array [qw/ a b c d e/],); my $dataref = \@data; my @all; push @all,@$dataref; #not sure of need for this? but works. print Dumper \@all; print scalar @{$dataref->[0]},"\n"; #number of elements in row[0] # i.e. "5" print $dataref->[0][1],"\n"; #prints "two", 2nd element of row +0 foreach my $element (@{$dataref->[0]}) #each element of row [0] { print "$element "; } print "\n\n"; foreach my $row_ref (@all) #deal with references to rows { print "number of elements = ".scalar @$row_ref," @$row_ref\n"; # of course number of elements will be the same in each row, #so only need to check for row[0] as above } __END__ $VAR1 = [ [ 'one', 'two', 'three', 'four', 'five' ], [ 'a', 'b', 'c', 'd', 'e' ] ]; 5 two one two three four five number of elements = 5 one two three four five number of elements = 5 a b c d e
    To find "how many columns", check the number of elements that are in the row 0 of the data array.
Re: Pushing rows of data into array
by Anonymous Monk on Apr 18, 2016 at 20:43 UTC
    push @all, @{ $rows }[0 .. $n-1]; # for appropriate $n

      Why not just
          push @all, @$rows;
      and forget about $n?


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

      How would you get the value for $n?
Re: Pushing rows of data into array
by BillKSmith (Monsignor) on Apr 19, 2016 at 13:50 UTC
    This suggestion is similar to Marshall's but starting with a CSV file.
    use strict; use warnings; use Data::Dumper; my @headers = split /\s/, <DATA>; print Dumper \@headers; my $length = @headers; my @all; while (my $line = <DATA>){ my $row = [split /\s/, $line, $length]; push @all, $row; } print Dumper \@all; __DATA__ col1 col2 col3 col4 a b c d 1 2 3 4
    Bill