Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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 .push @all, [ $rows->[0], $rows->[1], $rows->[2], $rows->[3], ];
Thanks for helping out if possible!#!/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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pushing rows of data into array
by Marshall (Canon) on Apr 18, 2016 at 22:30 UTC | |
|
Re: Pushing rows of data into array
by Anonymous Monk on Apr 18, 2016 at 20:43 UTC | |
by AnomalousMonk (Archbishop) on Apr 18, 2016 at 22:24 UTC | |
by Anonymous Monk on Apr 18, 2016 at 20:54 UTC | |
by Anonymous Monk on Apr 18, 2016 at 21:11 UTC | |
|
Re: Pushing rows of data into array
by BillKSmith (Monsignor) on Apr 19, 2016 at 13:50 UTC |