Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Splitting Multiple files into arrays.

by devbond (Novice)
on Jul 20, 2013 at 20:42 UTC ( #1045467=perlquestion: print w/ replies, xml ) Need Help??
devbond has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to read a file and split(by space) into an array called values and finally puttin it into a array table.I am able to do it for a single file but i want to split 8 files in a loop one after the other into 8 different arrays..table1 table2....table 8.

But the following code below is not working. Please advise.

for (my $i = 1 ;$i <=8; $i++) { open (MYFILE,"server$i"); while (<MYFILE>) { chomp; my @values = split " ",$_; push @{ "table$i"},\@values; } }

Comment on Splitting Multiple files into arrays.
Download Code
Re: Splitting Multiple files into arrays.
by mtmcc (Hermit) on Jul 20, 2013 at 20:55 UTC
    What does the data in your files look like, and what outcome do you expect to get?
      Date Value1 value2 value3 value 4 Date Value1 value2 value3 value 4 .. .. .. .. .. upto 25 times. i want to extract each value into an array element
Re: Splitting Multiple files into arrays.
by rjt (Deacon) on Jul 20, 2013 at 21:24 UTC
    But the following code below is not working.
        push @{ "table$i"}

    That's because you're trying to build a string and use it as an array ref. I strongly urge you to re-think your need to put everything into @array1, @array2, ...; this sort of logic is almost never a good idea. Use an array of array refs instead:

    use 5.012; use warnings; use autodie; my @array; for (glob 'file*.txt') { local $/; # Slurp my ($num) = /(\d+)\.txt$/g; open my $fh, '<', $_; $array[$num] = [ split /\s/m, <$fh> ]; } $" = '> <'; say "\$array[$_] = <@{$array[$_]}>" for 1..$#array;

    Output:

    $array[1] = <foo> <bar> <baz> $array[2] = <one> <two> <three> <four> <five> <six> $array[3] = <apple> <pear> <mango>

    But even at that, you may want to consider a hash of filenames, instead, as a more general solution:

    my %hash; for my $file (glob 'file*.txt') { local $/; # Slurp open my $fh, '<', $file; $hash{$file} = [ split /\s/m, <$fh> ]; } $" = '> <'; say "\$hash[$_] = <@{$hash{$_}}>" for keys %hash;

    Input files used

      I have a 2D File like the following and i have 8 such files which i have to put into 8 Arrays. 00:00 2453 10319 0 0 01:00 1565 626 0 0 02:00 1171 4195 0 0 03:00 1117 3597 4 0 04:00 1494 569 0 0 05:00 2873 0900 0 0 06:00 4899 2368 0 0 07:00 4251 9664 0 0 08:00 5791 8910 0 0 09:00 7068 0138 0 0 10:00 8464 3881 0 0 11:00 9610 5291 0 0 12:00 1017 8634 0 0 13:00 1090 8203 0 0 14:00 1322 9165 0 0

        That data is tab-delimited, not space-delimited. I'd use Text::CSV for this.

        use Text::CSV; my $csv = Text::CSV->new({ sep_char = "\t" }); my $array_ref = $csv->getline_all($fh); # OR... my @array = @{ $csv->getline_all($fh) };

        You can easily adapt the loop constructs I suggested, above, or many of the other suggestions given by others, with this approach. Good luck.

Re: Splitting Multiple files into arrays.
by jwkrahn (Monsignor) on Jul 20, 2013 at 21:49 UTC
    I am trying to read a file and split(by space) into an array called values and finally puttin it into a array table.I am able to do it for a single file but i want to split 8 files in a loop one after the other into 8 different arrays..table1 table2....table 8. But the following code below is not working. Please advise. for (my $i = 1 ;$i <=8; $i++) { open (MYFILE,"server$i"); while (<MYFILE>) { chomp; my @values = split " ",$_; push @{ "table$i"},\@values; } }
    use warnings; use strict; @ARGV = map "server$_", 1 .. 8; my %data; while ( <> ) { push @{ $data{ $ARGV } }, [ split ]; }
Re: Splitting Multiple files into arrays.
by mtmcc (Hermit) on Jul 20, 2013 at 21:50 UTC
    Maybe something like this?:

    #! /usr/bin/perl use strict; use warnings; my $x = 0; my %values; my @lines; my $lineNumber =0; my %parentHash; my @values; for ($x=1; $x<=6; $x += 1) { open (OUT, "<", "server$x"); while (<OUT>) { chomp; @lines = split(" ", $_); push (@{$parentHash{"server$x"}[$lineNumber]}, @lines) +; $lineNumber += 1; } $lineNumber = 0; close OUT; } #file line my @derefencedArray = @{$parentHash{"server2"}[2]}; print STDERR "@derefencedArray\n";

Re: Splitting Multiple files into arrays.
by Laurent_R (Vicar) on Jul 20, 2013 at 23:03 UTC

    Please format your code to make it readable. This is your code correctly reformatted.

    for (my $i = 1 ;$i <=8; $i++) { open (MYFILE,"server$i"); while (<MYFILE>) { chomp; my @values = split " ",$_; push @{ "table$i"},\@values; } }
    In short, you are using symbolic references, which was right 15 years ago, but is deprecated and probably wrong today.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1045467]
Approved by rjt
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2014-07-10 09:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (203 votes), past polls