On Sat, 16 Feb 2013 18:16:33 -0600 Ignoramus329 <> wrote:
I> As I always say 'use strict; use warnings', I have this statement in my program:
I> $is_freight = 1
I> if $ebay_record
I> && $ebay_record->{ShippingDetails}
I> && $ebay_record->{ShippingDetails}->{ShippingServiceOptions}
I> && $ebay_record->{ShippingDetails}->{ShippingServiceOptions}->{ShippingService}
I> && $ebay_record->{ShippingDetails}->{ShippingServiceOptions}->{ShippingService} eq 'Freight';
I> This is a correct statement and it does not give errors or warnings,
I> however, I would like to know if I can write it in a shorter way.
I have a function to do this, which recursively searches a hashref. It
has some array search logic as well; just remove the second part if you
don't want that (I needed it for parsing some XML data). You could use
Data::Match instead, but it's really not a difficult task either way,
and I like my interface better.
You call it like this for your case:
hashref_search($ref, qw/ShippingDetails ShippingServiceOptions ShippingService/);
and just check that the return is defined and equal to 'Freight'.
Hope that helps...
Ted
#+begin_src perl
sub hashref_search
{
my $ref = shift @_;
my $k = shift @_;
if (ref $ref eq 'HASH' && exists $ref->{$k})
{
if (scalar @_ > 0) # dig further
{
return hashref_search($ref->{$k}, @_);
}
else
{
return $ref->{$k};
}
}
if (ref $ref eq 'ARRAY' && ref $k eq 'HASH') # search an array
{
foreach my $item (@$ref)
{
foreach my $probe (keys %$k)
{
if (ref $item eq 'HASH' &&
exists $item->{$probe})
{
# if the value is undef...
return $item unless defined $k->{$probe};
# or it matches the probe
return $item if $item->{$probe} eq $k->{$probe};
}
}
}
}
return undef;
}
#+end_src
|