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: Beginner:
create a dbm with a hash, that has a key, with 2 values

 



dunsta
Novice

May 19, 2013, 11:53 PM

Post #1 of 7 (467 views)
create a dbm with a hash, that has a key, with 2 values Can't Post

Hi

I want to create a dbm that takes input in the form of something like this


Code
my $var={ 
'one' => {
'steve' => 'Steve@steve.com',
'rei' => 'rei@rei.com',
},
'two' =>{
'naomi' => 'naomi@naomi.com',
'rei' => 'rei@rei.com'
},
};


but a dbm takes a %hash, not a $var, so I am not sure how to go about getting the result I want.

Basically, I want to have different groups, and in each group I can have a name value pair.

Thanks for any input.
This is what I have attempted so far:

Code
my(%mailList2, $name, $email, $group); 

dbmopen(%mailList2, "mailingLists",0644) || die "Cannot open DBM: $!";

print "Enter a number: ";
chomp($group=<STDIN>);
print "Enter a name: ";
chomp($name = <STDIN>);
print "Enter an email: ";
chomp($email = <STDIN>);

$mailList2{$group}{$name} .= $email;



(This post was edited by dunsta on May 20, 2013, 2:54 AM)


Laurent_R
Veteran / Moderator

May 20, 2013, 3:15 AM

Post #2 of 7 (463 views)
Re: [dunsta] create a dbm with a hash, that has a key, with 2 values [In reply to] Can't Post

I am not sure of what you are trying to do, but your $var variable is a hash ref. Perhaps you could simply change it into a hash:


Code
my %var=(  
'one' => {
'steve' => 'Steve@steve.com',
'rei' => 'rei@rei.com',
},
'two' =>{
'naomi' => 'naomi@naomi.com',
'rei' => 'rei@rei.com'
},
);


Else, you could store that variable as a value into another hash.


dunsta
Novice

May 20, 2013, 3:26 AM

Post #3 of 7 (461 views)
Re: [Laurent_R] create a dbm with a hash, that has a key, with 2 values [In reply to] Can't Post

Hi and Thanks for the reply.

I am trying to use DBM to create a hash that looks like the VAR in my example.
But when I create it like this:


Code
my(%mailList2, $name, $email, $group); 

dbmopen(%mailList2, "mailingLists2",0644) || die "Cannot open DBM: $!";

print "Enter a number: ";
chomp($group=<STDIN>);
print "Enter a name: ";
chomp($name = <STDIN>);
print "Enter an email: ";
chomp($email = <STDIN>);

$mailList2{$group}{$name} = $email;


and try to access the data like this


Code
for my $group (keys %mailList2) { 

for my $name (keys %{$mailList2{$group}}) {

print $group, " -> ", $name, " -> ", $mailList2{$group}{$name}, "\n";

}

}


The output is only the input from the latest run, the data does not seem to be persisting.


(This post was edited by dunsta on May 20, 2013, 4:30 AM)


BillKSmith
Veteran

May 20, 2013, 5:29 AM

Post #4 of 7 (455 views)
Re: [dunsta] create a dbm with a hash, that has a key, with 2 values [In reply to] Can't Post

The function dbmopen does not store complex data. The book "Perl Cookbook" recommends using the module MLDBM and discusses it at some length. I have no personal experience to add to that.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

May 20, 2013, 7:31 AM

Post #5 of 7 (452 views)
Re: [BillKSmith] create a dbm with a hash, that has a key, with 2 values [In reply to] Can't Post


In Reply To
The function dbmopen does not store complex data.


Right, I knew it (I tried it once), but sort of did not pay attention to that.


recruiter
User

May 20, 2013, 8:27 AM

Post #6 of 7 (442 views)
Re: [Laurent_R] create a dbm with a hash, that has a key, with 2 values [In reply to] Can't Post

With dbm I think you can use a basic hash to store your data and loop through it, you could combine your group number into the hash or just count the the lines for your input. I think something like this.


Code
my %mailList2;   
dbmopen(%mailList2, "mailingLists2",0644) || die "Cannot open DBM: $!";

print "Enter a name: ";
chomp(my $name = <STDIN>);
print "Enter an email: ";
chomp(my $email = <STDIN>);

$mailList2{$name} = $email;

my $n = 0;
for my $i ( keys %mailList2 ) {
++$n;
print "$n -> $i -> $mailList2{$i}\n";
}



dunsta
Novice

May 20, 2013, 11:55 PM

Post #7 of 7 (430 views)
Re: [hwnd] create a dbm with a hash, that has a key, with 2 values [In reply to] Can't Post

actaully, $group as a number was just a testing scenario. I solved my problem by using $group as the key, and a string concat of name, email as the Value. Then when need the data, I pull the values into an array and pop them out 2 at a time. This gives me name/email per group.

 
 


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

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