CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Multi-dimensional Hashes and value searches

 



Iconx
Novice

Apr 14, 2014, 2:40 PM

Post #1 of 4 (10701 views)
Multi-dimensional Hashes and value searches Can't Post

I've messed around with this all kinds and read a few things but not even sure if this would work. Do I have to do a foreach/if/else?

Q: Is it possible to search a multi-dimensional hash for a value?

Overview:
I have a script that reads a directory and the contents of a each file in that directory, it then loads it into a hash that looks like this:

Code
%{ $record{$id_num} } = ( 
filepath => $filepath
uid => $uid,
acctname => $acct_name,
type => $type,
status => $status,
)

I intend to use this hash for a multiple different purposes one of which is to pull that accounts in a specific status. I know I could break it apart with a foreach and if/else to get status but isnt there a way to just make the hash print everything it has on an ID number for all status's having say a V? Hash looks like this in dumper:


Code
Print Dumper: 
$VAR1355 = '250198';
$VAR1356 = {
'status' => 'V',
'filepath' => '../records/active/250498.user73a',
'acctname' => 'user73a',
'type' => 'Prod',
'uid' => '3001',
};

Question: So is it possible so find all entries in the hash that have a status V and be able to print everything that goes with that account?

PS - I know I can do something like this with the ID cause its the key like
print %{ $record{250198} }

I would have thought somehow chaining the hash like %{ $record{$id_num}{'v' } or something would produce this but I get why it doesnt, but I cant make status the key cause its not unique.

Make sense?

Thanks,
- Iconx


Iconx
Novice

Apr 14, 2014, 3:52 PM

Post #2 of 4 (10699 views)
Re: [Iconx] Multi-dimensional Hashes and value searches [In reply to] Can't Post

FYI - this is supposed to be for a perl module.

What do you think?

I found in addition to making ${$record}{$id_num}, in the foreach loop of reading the file, I could do it like this and also make,


Code
%{$statuschk{$status}{$id_num} = (filepath=>$filepath,uid=>$uid.......etc)

and outside the foreach :


Code
my $value = 'V'; 
print $statuschk{$value};


shows me all the status's with V.

Is there a high cost to building multiple hashes on one read of a file? Is this the best way?


FishMonger
Veteran / Moderator

Apr 14, 2014, 4:32 PM

Post #3 of 4 (10698 views)
Re: [Iconx] Multi-dimensional Hashes and value searches [In reply to] Can't Post


Quote
Q: Is it possible to search a multi-dimensional hash for a value?

Of course it's possible. It's also very easy.


Quote
Question: So is it possible so find all entries in the hash that have a status V and be able to print everything that goes with that account?

See the above answer.

Other than that, I can't figure out what you're tying to ask.

Your Dumper output tells me that you're not being consistent in how you're assigning the %record data. You should fix that issue before trying to write the code that outputs the data within the hash.


Laurent_R
Veteran / Moderator

Apr 15, 2014, 2:15 AM

Post #4 of 4 (10693 views)
Re: [Iconx] Multi-dimensional Hashes and value searches [In reply to] Can't Post

It seems that you don't have a simple hash, but a hash of hashes (HoH). If you are unsure on how to use this type of structure, you probably need to read the perldsc document: http://perldoc.perl.org/perldsc.html.

Your need is not entirely clear to me, but you could try something like this:

Code
 
use strict;
use warnings;

my %outerhash = (
one => {
"status" => "V",
"filepath" => "../records/active/250498.user73a",
"acctname" => "user73a",
"type" => "Prod",
"uid" => "3001",
},
two => {
"status" => "X",
"filepath" => "../records/active/250498.user73a",
"acctname" => "user73a",
"type" => "Prod",
"uid" => "3002",
},
three => {
"status" => "V",
"filepath" => "../records/active/250498.user73a",
"acctname" => "user73a",
"type" => "Prod",
"uid" => "3003",
},
);
foreach my $hashref (values %outerhash) {
next unless defined $hashref->{status};
next if $hashref->{status} ne "V";
print "\n";
foreach my $key (keys %{$hashref}) {
print "$key => $hashref->{$key}\n";
}
}


This will print the following:


Code
acctname => user73a 
uid => 3003
status => V
type => Prod
filepath => ../records/active/250498.user73a

acctname => user73a
uid => 3001
status => V
type => Prod
filepath => ../records/active/250498.user73a


I hope this is more or less what you were looking for.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives