in reply to How can I read multiple lines starting with the same number and put in to a nested array and print it to a file?

Your examples appear to be sorted in reverse numerical order, rather than by column 9 as you state. Coincidentally, with this structure, that is the same as a reverse dictionary sort on the strings. You can save a lot of memory by storing them as strings instead of splitting them.

There are still a couple of ways to go with it. You can either (A.)read the file as an array of lines and do a custom sort, or else you can (B.)build an array of arrays indexed by the first digit and sorted by the remainder:

# data file is open to read on the FOO handle my @lines = <FOO>; # option A. my @lines = sort { substr( $a, 0, 1) cmp substr( $b, 0, 1) or substr( $b, 1) cmp substr( $a, 1) } @lines; # option B. my @ary; push @{$ary[ substr( $_, 0, 1)]}, substr( $_, 1) for @lines; @ary = map {[ reverse sort @$_ ]} @ary;
If you don't need fast selection grouped on the first digit, option A is the pick. it will be lighter on memory, and possibly faster.

If you need the row data as an array, split will give you that as needed.

Update: Re: your followup That would be option B. @{$ary[4]} is the sorted array of all entries starting with four. The four has been stripped for convenience in sorting, but since you know the index you're using, you can restore it for printing or whatever.

After Compline,
Zaxo

  • Comment on Re: How can I read multiple lines starting with the same number and put in to a nested array and print it to a file?
  • Select or Download Code

Replies are listed 'Best First'.
Re: Re: How can I read multiple lines starting with the same number and put in to a nested array and print it to a file?
by Anonymous Monk on Jun 24, 2002 at 03:29 UTC
    As I said I have no trouble puting lines into an array and sorting them the way I want, my trouble is how I can read multiple lines starting with the same number and put only those into an array (sort it print it) and continue. In my example read the first line then all the lines starting with that number( happens to be number 1), put them into an array, sort it, open output file file print it, get all the lines starting with next number which is 4 in this example, put them into an array, sort it , open the output file print it, and do this till the end of file. I need an algorithm, I feel quite stupid, I should be able to work this out, but my brain is not working at the moment! I can put the entire file into an array, but I do not want to that, because of memory problems. Thanks a lot for all the responds.
Re: Re: How can I read multiple lines starting with the same number and put in to a nested array and print it to a file?
by Anonymous Monk on Jun 24, 2002 at 09:04 UTC
    Unfortunately I do not know what the starting line numbers are. I have 3 GB file and I can not put it into a big array. The example above is extremely simplified example. I might have element 10 (which is first number in the line) and 20 lines starting with that number , The next number might be 10000 and I might have 20 lines starting with that number. I might have 20000 elements, which means 20000 different elements and 20000 * 20 lines (lets say there are 20 lines starting with the same element number). Basically I just would not know what the element numbers are. I need to find out as I am going through the file.