Re: Sorting a nested array (table) (alphabetically)
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 29 Sep 2005 04:51:46 -0700
nstott1982@xxxxxxxxx wrote:
> bigest problem i have hit is sorting a nested array, i would appriciate
> any help given.
>
> here is the code i use to read the file into the array
>
>
> $ID = 1;
> open (InData, $DataStore);
Always check the return value of open(). Currently, you have no way of
knowing whether or not $DataStore was successfully opened.
open InData, '<', $DataStore or die "Could not open $DataStore: $!\n";
> while ($CurrentLine = <InData>) {
> @DataTempA = split(/¤/,$CurrentLine);
> @DataTemp[$ID] = [ @DataTempA ];
You are obviously not using warnings. Please read the posting
guidelines for this group.
> $ID++;
>
> }
> $DataTemp[0][0] = $ID;
> close(InData);
>
>
> i would like to sort by different columns (@DataTemp[Row][Col])
I don't understand what this means. Can you provide some sample input
and desired output?
> i cant seem to find anything on the net which i can understand,
Perhaps you could show us what you did find, and let us know how you
were unable to understand it, so that we could clarify it for you?
Have you read Perl's built in documentation for sort?
perldoc -f sort
http://perldoc.perl.org/functions/sort.html
perldoc -q sort
http://perldoc.perl.org/perlfaq4.html#How-do-I-sort-an-array-by-%28anything%29%3F
> im not expecting anyone to just write me the code that will work
> (although would be sweet) but just a point in the right direction would
> be good
In general, you provide sort with a block which compares two elements
of your list (which Perl assigns to $a and $b), and returns -1 if $a
should come first, 1 if $b should come first, and 0 if it doesn't
matter.
In your case, $a and $b would be array references (as each element of
the overall array is an array reference). It's up to you to decide how
to determine which of two array references should come first.
Without knowing exactly how you want your 2-d array sorted, however,
it's hard to give an example.
Paul Lalli
.
- References:
- Sorting a nested array (table) (alphabetically)
- From: nstott1982
- Sorting a nested array (table) (alphabetically)
- Prev by Date: Re: Wierdness
- Next by Date: Re: switching workspaces in CDE using perl
- Previous by thread: Sorting a nested array (table) (alphabetically)
- Next by thread: FAQ 6.10 How do I use a regular expression to strip C style comments from a file?
- Index(es):
Relevant Pages
|
|