R.A. Epigonos et al.

[perl] リファレンスに関するテスト(2)

リファレンスとは何ぞやということがわかっていなかったので、テストコードを書いてみた。

# generate unnamed array by [ ]@LoL = ( ["1" , "21", "21" ],
                ["5" , "33", "43" ],
                ["12", "15", "21" ] );
# in array @LoL, thera are references of each element quarted by [ ]print "@LoL\n";# that is why, each refferance $_ operated ->[0] means the real value of 1,5,12# and, operated ->[1] and ->[2] are understanded in the same way.foreach (@LoL){
        print $_."\t".$_->[0]."\t".$_->[1]."\t".$_->[2]."\n";
}# to sort values on 3rd column, How to?# first, get values and refferences on 3rd column# and generte hash %column like key:refference value:value.foreach (@LoL){
        $column{$_} = $_->[2];
}print %column;print "\n";# second, sort values in hash and get the rasult in array @column_sorted.# third, foreach my $value (sort {$a<=>$b} values %column){
        foreach my $key ( keys %column ){
                if ( $value eq $column{$key} ){
                        $value2 = delete $column{$key};
                        print "$value2\t$key\n";
                        push @LoL2,$key;
                        last;
                }
        }
}print "@LoL2\n";# on the end here is a sorted LoL by the numberes on 3rd columnforeach my $ref (@LoL2){
        foreach (@LoL){
                if ($_ eq $ref){
                        print $_."\t".$_->[0]."\t".$_->[1]."\t".$_->[2]."\n";
                        last;
                }
        }
}
exit;

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us

ChangeLog

  1. Posted: 2007-10-02T18:44:16+09:00
  2. Modified: 2007-10-02T04:23:28+09:00
  3. Generated: 2024-12-16T23:09:16+09:00