The Elite Noob has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I was learning about file I/O in Perl. So say i have the following text file. let's call it "text.txt".
3 3 3 6 5 2 4 5 1 5 7 -- 2 4 1 4 5 9 3 8 6 8 -- 5 3 1 5 3 7 3 6 4 7 9 1 2 3 5 7 1
So what I want to do is read the first line. then create a multi-array the size using the first number to figure out how many lines there. And the second number is how many elements per array. so the First set of data would have an array. "@array[3][3]".Now i need to read the next 3 lines and put the digits of each line in the array. so
@array[0][0] = 3; @array[0][1] = 6; @array[0][2] = 5; @array[1][0] = 2; @array[1][1] = 4; @array[1][2] = 5;

Then when it encounters "--" in a file it stops reading. Then it calculates based off the array. Then when it starts reading again. It is after the first "--". So the program creates an array "@array[2][4]". And this continues.

So anyone any idea how i start reading again from where i left off? Thanks and all help is appreciated!

Replies are listed 'Best First'.
Re: Reading Input from files and separating them into arrays.
by wind (Priest) on Mar 29, 2011 at 23:45 UTC

    If you're separating your data by --, you don't even need the size information.

    use Data::Dumper; use strict; my @array; my $i = 0; while (<DATA>) { chomp; # Skip header row if (!$i++) { next; # End of Data Structure. } elsif (/^-*$/) { print Dumper(\@array); @array = (); $i = 0; # Add to data } else { push @array, [split / /]; } } print Dumper(\@array); __DATA__ 3 3 3 6 5 2 4 5 1 5 7 -- 2 4 1 4 5 9 3 8 6 8 -- 5 3 1 5 3 7 3 6 4 7 9 1 2 3 5 7 1
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Reading Input from files and separating them into arrays.
by GrandFather (Saint) on Mar 30, 2011 at 02:40 UTC

    Show us what you have tried so far. You may find reading the documentation for open and perllol helpful.

    True laziness is hard work
      Sure thing. I'm post this evening. Maybe in about 2-3 hours.