Thnaks a lot Jos. The idea of reading the array into a hash is quite appealing, and 
simple too. But I have a small problem with this: 

What if I want to sort on the second column of the array? Or if there are more
than two columns? 

Say we have: 
my @arr = qw(
1 2 3 4
4 5 6 7
5 6 7 8
1 2 3 4
3 1 8 3  
); 
And if I wanted to sort on the 3rd column. 
Then I could write: 

@sorted  = sort { $a->[2] <=> $b->[2] } @arr; 

# or 'cmp' if the sorting is to be lexical. 

This should've worked. But why do I get a warning: 

Use of uninitialized value at ./mk2_ratingchangedb.pl line 39, <F> chunk 8. 

Here's the snippet of my code: 

    29      my(@dummy, @dummy2, @wed);
    30      for ($i=1; $i<=$N; $i++) {
    31          $dummy[$i][0] = &ParseDate($data{$key}[$i][0]);
    32          if (! $dummy[$i][0]) {
    33            warn "Could not parse $data{$key}[$i][0]\n";
    34          }
    35          $dummy[$i][1] = $data{$key}[$i][1];
    36      }
    37      @wed = @dummy;
    38      &matrix_printer ("pre sort", @wed);
    39      @dummy2 = sort { $a->[0] cmp  $b->[0] } @wed;
    40      printf " ----------------------------\n";
    41      &matrix_printer ("post sort", @dummy2);
    42  }     

What could be wrong? ( The array @wed was REQUIRED. Otherwise my code
didn't work at all).

Thanks for the prompt response, 

-tir

------------------------------------------------------------

On Sat, 16 Jun 2001, Jos I. Boumans wrote:

>  ok, so if i get this right, @dummy has the following format:
> 
> my @dummy = qw(
>         1996013100:00:00        MAAA
>         2000081100:00:00        MA-
>         1997063000:00:00        MAAA
>         1998122200:00:00        MAA
>         2000112400:00:00        MD
> );
> 
> now we have that established, let's say that as soon as you think 'gee i
> need to SORT something on ONE VALUE in this data' you should probably be
> using a hash.
> so that's what we'll do
> 
> what we'll do is read every line of @dummy, split it on white space and put
> that as key/value pairs in a hash, like so:
>     my %hash = map { split /\s/ } @dummy;
> 
> this is equivalent to the more verbose:
>     my @data;
>     for my $element (@dummy) {
>         my ($key, $value) = split(/\s/, $element);
>         push(@datay, $key, $value);
>     }
>     my %hash = @data;
> 
> as you can see, the above is quite a bit more writing, slow and ugly then
> the map solution, but it's more 'logical' to read for most.
> also note that there's no objection to saying: %hash =
> @array_with_key_value_pairs
> the other way around is a bit more tricky, seeing hashes keep their
> key/value pairs seemingly unordened, wheras arrays keep indexes... so you
> can't say:
>     %hash = @array_with_key_value_pairs;
>     @copy_of_array_with_key_value_pairs = %hash;
> and assume that
>     @copy_of_array_with_key_value_pairs eq @array_with_key_value_pairs;
> 
> having said that, let's see how we can get our info out of the hash like we
> want to:
>     for (sort keys %hash) {print "$_ has value $hash{$_}\n"}
> 
> the above will print:
>     1996013100:00:00 is MAAA
>     1997063000:00:00 is MAAA
>     1998122200:00:00 is MAA
>     2000081100:00:00 is MA-
>     2000112400:00:00 is MD
> 
> Hope this does what you want,
> 
> hth,
> 
> Jos Boumans
> 
> 
> <snipping stuff>
> > Folks,
> > # How do I sort an array by one of it's fields?
> >
> > # @dummy before sorting has this:
> >
> >         1996013100:00:00        MAAA
> >         2000081100:00:00        MA-
> >         1997063000:00:00        MAAA
> >         1998122200:00:00        MAA
> >         2000112400:00:00        MD
> 
> > I would like to sort by it by the first column, which is a date read by
> >     ParseDate fron Date::Manip.
> </snipping stuff>
> 
> 

Tirthankar C.Patnaik
Research Scholar
Indira Gandhi Institute of Development Research  Ring:+91-22-840 0919/20/21
Goregaon East                                    Off:- x593 Res: x675  
Mumbai 400 065                                   Fax:  91-022-8402752            
India. http://www.igidr.ac.in                    email:[EMAIL PROTECTED] 

Never argue with an idiot. They drag you down to their level, then
beat you with experience. -dilbert 

---------------------------------------------------------------------------      

Reply via email to