[ABE.pm] Curious

Faber J. Fedor faber at linuxnj.com
Mon Dec 18 18:56:34 PST 2006


On 18/12/06 14:42 -0500, Ricardo SIGNES wrote:
> As has been noted, it's a context issue.  I would advise that even this
> solution:
> 
>   my ($date) = keys %$dataHRef;
> 
> is not great, since if there is more than one key, 


I never would have thought of this.  It worked well in one situation
where I do have just one key.

Thanks to all who responded.

As for my previous problem, I worked around it using a foreach.  Kinda
stupid to use for one key, but it came in handy later when there was a
case with multiple keys!

I do have a stylistic(?) question.  

My hash (again):
0  HASH(0x870fa08)
  19890131 => HASH(0x87e699c)
     1 => 0.311103
     10 => 0.628309
     2 => '-0.412132'
     3 => 0.376397
     4 => '-0.020074'
     5 => 0.700074
     6 => '-0.172132'
     7 => '-0.652206'
     8 => '-0.673162'
     9 => '-1.566985'

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

The 'my ($date) = keys %$hashref;' looks like nothing more than an
intermediate variable which we should be able to get rid of.  Can we?

I would try to replace '$hashref->{$date}' with '$hashref->{keys
%$hashref}' but not only does it get computed on each pass for no good
reason, it just looks tacky.

-- 
 
Regards,
 
Faber Fedor
President
Linux New Jersey, Inc.
908-320-0357
800-706-0701

http://www.linuxnj.com





More information about the ABE-pm mailing list