2013年11月21日 星期四

Perl Arrays of Hashes & array sort

Reference:
http://docstore.mik.ua/orelly/perl/prog3/ch09_03.htm

@AoH = ();
push @AoH, { ID => "1201", range => 7, price => 50 };
push @AoH, { ID => "1202", range => 5, price => 20 };
push @AoH, { ID => "1203", range => 6, price => 30 };
print $AoH[1]{price};

# hash %row
$row{ID} = "1204";
$row{range} = 4.3;
$row{price} = 70;
push @AoH, \%row;    # add %row to array by address
print $AoH[4]{range};
print $#AoH;   # array items number

# sort array by range
@AoH = sort {$b->{range} <=> $a->{range}} @AoH;
print $AoH[0]{ID};
print $AoH[1]{ID};
print $AoH[2]{ID};

沒有留言:

張貼留言