As Zaxo has said you need to step back from your problem and rethink it through. Once you know what it is (as a whole) that you're trying to do, tell us and let us suggest solutions. Offering part solutions to us is great, but it's very hard for us to give you reasonable advice if you won't tell us what the overall picture should look like.
Having said that I'm going to take a wild guess at what your problem is and what you're trying to do to solve it and offer a solution to match that.
(Some of) The Problem
You want to read in a file and create a two dimensional array of the form:
Ideally you'd like to be able to change the size of each partition or add more partitions if necessary without having to change all the code.Library ( 0 -> [all the data from the file which starts with a + value between 1 and n] 1 -> [all the data from the file which starts with a + value between n and 2n] .... 7 -> [all the data from the file which starts with a + value between 7n and 8n] );
Is that correct? *jarich hopes so.* Try this.
#!/usr/local/bin/perl -w use strict; use Data::Dumper; my @column_info = ([ 1024, 1067 ], # Column 1 start stop [ 1068, 1093 ], # Column 2 start stop [ 1094, 1137 ], # Column 3 start stop [ 1138, 1163 ], # Column 4 start stop [ 1164, 1207 ], # Column 5 start stop [ 1208, 1233 ], # Column 6 start stop [ 1234, 1277 ], # Column 7 start stop [ 1278, 1321 ] ); # Column 8 start stop # Note that we don't have to cat this to read the data. # We can just open for reading, hence the "<". # Note also that we check the return value for open. # This is very very important. open(IN, "< /tmp/show_library.$$") or die "Failed to open /tmp/show_library.$$ for reading: $!"; my @Library; LINE: while(<IN>) { s/^ //; # works on $_ by default. chomp; # split on spaces and take the first value my $column = (split(' ', $_))[0]; for(my $i=0; $i < @column_info; $i++) { my ($start, $stop) = @{$column_info[$i]}; # if this is the right range, put it in our li +brary if($column >= $start && $column <= $stop) { push @{$Library[$i]}, $_; next LINE; } } # If we've got to here we haven't found the range for # this entry. We can do whatever we like, but here I # push it on to the very last element of Library, afte +r # the final range value push @{$Library[@Library]}, $_; } # To verify we have the data structure we wanted: print STDERR Dumper(@Library); # Note that the entries are not sorted by their inserted value. # That isn't too hard to resolve. foreach my $entry (@Library) { @$entry = sort {(split(' ', $a))[0] <=> (split(' ',$b))[0]} @$ +entry; } # To verify we have the ordering we wanted: print STDERR Dumper(@Library);
The structure I have here is much more simple than what you've suggested. I realise that I might be completely wrong about what you're trying to achieve but I hope that this might give you some assistance.
Of course, if all you're trying to do is to read in the file in such a way that the first 8th of it is in Library[0], and the second eigth is in Library[1] etc, that's a whole other story. :)
Hope it helps
jarich
Update:Whoops! I forgot to label the next. Have done so now.
In reply to Re: Problem reading data file
by jarich
in thread Problem reading data file
by coec
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |