Louis Pouzin wrote: > > That's slick. I like it. I feel getting better at climbing down hash trees. > One thing hangs me up. What's "{$s}++" doing at the end of: > $h{${$lis{$i}{$j}}[1]}{${$lis{$i}{$j}}[0]}{$s}++; > > {$s} looks like one hash deeper with key $s, sorted at the end of your script. > But then ++. Watsit ? > Another Perl trick I've got to learn ? > > Thanks a lot.
you know that $x++ postincrements $x? well, using it in things like $h{$x}++;#A you increment the key's related value, like $h{$x}=$h{$x}+1; So, using #A rather than $h{$x}=1;#B means that if any key occurs more than once, you get its frequency. $h{cat}=$h{cat}+1; $h{cat}=$h{cat}+1; $h{cat}=$h{cat}+1; print "$h{cat} cats\n"; $h{dog}++; $h{dog}++; $h{dog}++; print "$h{dog} dogs\n"; > - - > On Tue, 21 Jan 2003 16:39:39 -0800, Richard Cook wrote: > > >#!/usr/bin/perl -w > >BEGIN { $^W = 1 } > >use strict; > >my %lis; #age sex > >$lis{cat}{Zoe}=["9","female"]; > >$lis{dog}{Max}=["8","male"]; > >$lis{pig}{Joe}=["6","barrow"]; > >$lis{cat}{Sam}=["1","female"]; > >$lis{dog}{Bob}=["2","male"]; > >$lis{cat}{Kat}=["5","female"]; > >$lis{pig}{Sly}=["7","barrow"]; > >$lis{cat}{Tom}=["1","male"]; > >$lis{pig}{Zig}=["3","barrow"]; > > >print "-"x32,"\n"; > >my %h; > >for my $i (keys %lis){ > > for my $j (keys %{$lis{$i}}){ > > my $s = ", a pet $i named $j; "; > > $h{${$lis{$i}{$j}}[1]}{${$lis{$i}{$j}}[0]}{$s}++; > > } #sex age > >} > >for my $h (sort { $a cmp $b } keys %h){ > > print "sex $h: "; > > for my $i (sort {$a <=> $b} keys %{$h{$h}}){ > > print "age $i"; > > for my $s (sort keys %{$h{$h}{$i}}){ > > print "$s"; > > } > > } > > print "\n"; > >} > >#END