[ABE.pm] Curious

Ricardo SIGNES rjbs-perl-abe at lists.manxome.org
Mon Dec 18 20:38:37 PST 2006


* "Faber J. Fedor" <faber at linuxnj.com> [2006-12-18T21:56:34]
> I do have a stylistic(?) question.  
> 
> My hash (again):
> 0  HASH(0x870fa08)
>   19890131 => HASH(0x87e699c)
>      ...
>
> Yes, just one key.  I pass it to a function and I want to access the sub-hash
> (the 1 through 10 elements).  Is there a better way than this:
> 
> sub foo {
>     my ($hashref) = @_;
> 
>     my ($date) = keys %$hashref;
> 
>     for(my $i=1; $i<=10; $i++) {
>         $hashref->{$date}->{$i} = $moonphase * 2;
>     }
> } $ end of foo

Assuming that you're calling foo like this: foo($hashref)

You could, instead, write: foo($_) for values %$hashref

foo would then be defined:

  sub foo {
    my ($bucket) = @_;

    for my $i (1 .. 10) {
      $bucket{$i} = $moonphase * 2;
    }
  }

Since the values in $hashref are references, you can pass them around without
always giving the same referring structure to get at them.  Each copy of the
reference points to the same thing.

-- 
rjbs


More information about the ABE-pm mailing list