• RSS
  • Twitter
  • FaceBook

Security Forums

Log in

FAQ | Usergroups | Profile | Register | RSS | Posting Guidelines | Recent Posts

Using Multi dimensional hashs in Perl

Users browsing this topic:0 Security Fans, 0 Stealth Security Fans
Registered Security Fans: None
Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> Programming and More

View previous topic :: View next topic  
Author Message
tutaepaki
Trusted SF Member
Trusted SF Member


Joined: 02 May 2002
Posts: 3
Location: New Zealand

Offline

PostPosted: Thu Nov 07, 2002 4:31 am    Post subject: Using Multi dimensional hashs in Perl Reply with quote

I'm writing something for a collegue in Perl in which a multi dimensional
hash would be a nice solution. But I'm buggered if I can get them to work.
I've tried to find examples etc and read the man pages etc.
What I'd like to do is create a hash like....

$HASH{$target}{$interface} = $value

whilst reading a file. I'll used the data, and change some of it, and when
I'm finished write the hash back out to the file.
I get no syntax errors, and loading the hash appears to work, but I can't
work out how to process the hash sequentially at the end. Something like

while (($key,$value) = each %HASH)

returns the value I set in $interface above as the key, and HASH{0X87AB} in value. (or some such pointer(?) )

Does anyone have a decent tutorial or example of something like this?

I'm currently working around by creating a single dimensional array like

$HASH{$target~$interace} = $value

which works fine, but I don't like relying on a special char as a separator in case it's used somewhere else.

Thanks
Back to top
View user's profile Send private message
enigman
Just Arrived
Just Arrived


Joined: 09 Oct 2002
Posts: 0
Location: Sydney

Offline

PostPosted: Thu Nov 07, 2002 5:56 am    Post subject: Reply with quote

As you had thought, it is a pointer (to the second hash). When you print out the first hash it contains a reference to the address containing the second hash.

The following code sample is from the excellent book Perl by Example 3rd Edition by Ellie Quigley. Probably one of the best books for learning Perl (IMO). The following code demonstrates the concept with anonymous hashes but you should be able to adapt it. Let me know if you need further assistance.

Code:
#! /usr/bin/perl
# Program to demonstrate a hash containing anonymous hashes.
my $hashref ={
    Math=>{
   Anna => 100,
   Hao => 95,
   Rita => 85,
    },
    Science => {
   Sam => 78,
   Lou => 100,
   Vijay =>98,
    },
};
print "Anna got $hashref->{Math}->{Anna} on the Math test.\n";
$hashref->{Science}->{Lou}=90;
print "Lou's grade was changed to $hashref->{Science}->{Lou}.\n";
print "The nested hash of Math students and grades is: ";
print %{$hashref->{Math}}, "\n"; # prints the nested hash, Math
foreach $key (keys %{$hashref}){
    print "Outer key: $key \n";
    while(($nkey,$nvalue)=each(%{$hashref->{$key}})){
   printf "\tInner key: %-5s -- Value: %-8s\n",
   $nkey,$nvalue;
    }
}


hth
Enigman
---
Despite the high cost of living, it remains popular.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
Display posts from previous:   

Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> Programming and More All times are GMT + 2 Hours
Page 1 of 1


 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Looking for more Windows Networking info?

Sign up to the WindowsNetworking.com Monthly Newsletter, written by Enterprise Security MVP Deb Shinder, containing news, the hottest tips, Networking links of the month and much more. Subscribe today and don't miss a thing!
View a sample newsletter.

Become a WindowsNetworking.com member!

Discuss your Windows Networking issues with thousands of other Windows Newtorking experts. Click here to join!

Community Area

Log in | Register

Readers' Choice

Which is your preferred data recovery solution?

Follow TechGenix on Twitter