Re: Sorting a nested array (table) (alphabetically)



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

.



Relevant Pages

  • Re: order a semicolon-separated data file by a value of a column
    ... >>My suggestion would be to read each element, store each line in an array ... >>reference, store each reference in a larger array, sort the array by the ... type 'perldoc ... > references, I read the lama book and it does't mention references. ...
    (comp.lang.perl.misc)
  • Re: "Sorting" assignment
    ... And many others prefer to call partition exchange because "quicksort" ... bin B depending on whether it is greater than, ... If the array is already sorted, this means that you end up ... attempt to sort them. ...
    (comp.programming)
  • Re: A Fast sorting algorithm for almost sorted data
    ... far my compressor has potential but is nowhere near ready. ... It does however make heavy use of sorting. ... which I am currently calling Run sort. ... entire selected run can be added to the sorted output array. ...
    (comp.compression)
  • A Fast sorting algorithm for almost sorted data
    ... which I am currently calling Run sort. ... entire selected run can be added to the sorted output array. ... public class RunSort implements Comparator ... public static void sort(Comparable a, int start,int end) ...
    (comp.compression)
  • Re: Save & Sort
    ... You can copy your array to a scratch ... "Heap" sort. ... Dim lst As Long ... Dim tmp As String ...
    (microsoft.public.excel.programming)