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!

In reply to Pushing rows of data into array by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.