[Omaha.pm] My first hash slice
Jay Hannah
jay at jays.net
Thu Oct 2 06:02:28 PDT 2008
On Oct 1, 2008, at 2:34 PM, <jay at jays.net> wrote:
> while (my $href = $sth->fetchrow_hashref) {
> foreach my $key (keys %$href) {
> $href->{$key} =~ s/\s+$//;
> }
> $DB::single = 1;
> my $key = join "|", @$href{ qw(rate_cat room_code
> hrms_room_cat stod etod) };
> print "$key\n";
> unless ($self->_Band2s->{$key}) {
> my $b2 = Omni2::Control::OmniCRS::HRMS::Band2->new($href);
> $self->_Band2s->{$key} = $b2;
> }
> }
On Oct 1, 2008, at 2:48 PM, Andy Lester wrote:
> Now, note that you have a $key in the foreach loop, AND you're
> overriding it inside the loop, too. That's confusing.
Ya. That first $key is throwaway. Only used for 1 line of code.
I'm thinking changing the second $key to some other variable name
would lead a reader to believe that I still care about the first
$key, which I don't.
Would this be better or worse?
map { $href->{$_} =~ s/\s+$// } keys %$href;
Worse, I suspect. Or
for (values %$href) { s/\s+$// }
Looks like those both work. I'm thinking the last one is best.
> Also, how about instead of
>
> print "$key\n";
>
> you use
>
> say $key;
That's a 5.10 feature, yes? We're not a 5.10 shop yet.
Thanks,
j
More information about the Omaha-pm
mailing list