Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

file1.txt: foo bar baz file2.txt: one two three four five six file3.txt: apple pear mango

In reply to Re: Splitting Multiple files into arrays. by rjt
in thread Splitting Multiple files into arrays. by devbond

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-23 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found