Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Re: Is there a shorter, more elegant way to write this deep hash lookup statement

Reply
Thread Tools

Re: Is there a shorter, more elegant way to write this deep hash lookup statement

 
 
Ted Zlatanov
Guest
Posts: n/a
 
      02-21-2013
On Sat, 16 Feb 2013 18:16:33 -0600 Ignoramus329 <ignoramus329@NOSPAM.329.invalid> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Is there a shorter, more elegant way to write this deep hashlookup statement Dr.Ruud Perl Misc 1 02-21-2013 04:37 PM
Re: Is there a shorter, more elegant way to write this deep hashlookup statement C.DeRykus Perl Misc 0 02-19-2013 07:40 PM
Re: Is there a shorter, more elegant way to write this deep hash lookup statement George Mpouras Perl Misc 0 02-17-2013 12:07 PM
What's a good way to find whether a hash is a subset of another hash (deep)? dblock Ruby 2 10-09-2011 08:37 PM
Is there a more elegant way to do this? Kamilche Python 7 06-29-2004 04:33 AM



Advertisments