Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need A Good Perl Coder to check this out please... 1

Status
Not open for further replies.

mrchaos70

Technical User
Apr 15, 2002
7
US


Hello, Im still working on learning perl and have run into something I dont under stand.

Below are 2 subs, 1 that enters data into a record, and 1 that shows all records.

with only 2 pices of data, like NAME and EMAIL, it all looks great, ONCE I add a 3rd peice of data, it still works but im getting a weird char printing out on the screen. I dont know what this is or where it is comming from. I have a feeling it has to do with this line of code
$currentValue = $CUSTLIST1{$name,$phonenum};

Im sure this is easy for most of you, but for a newbie, i have spent 2 days online, and down loaded some perl help books. Just cant get it.



sub enterData {

dbmopen (%CUSTLIST1, "database1", 0644) ||
die "Cannot open database.\n";

system('cls');
print "\n\tEnter your first and last name:\n\t";
$name = <STDIN>;
chomp($name);

print &quot;\n\tEnter Email Address:\n\t&quot;;
$email = <STDIN>;
chomp($email);


print &quot;\n\tEnter Phone number: \n\t&quot;;
$phonenum =<STDIN>;
chomp($phonenum);

$CUSTLIST1{$name, $phonenum} = $email;



$currentValue = $CUSTLIST1{$name,$phonenum};
print&quot;\n\tEntry made: $currentValue\n\t&quot;;
dbmclose (CUSTLIST1);
system('cls');
next;

}



#---------------SHOW ALL RECORDS SUB

sub showAll{
dbmopen(%CUSTLIST1, &quot;database1&quot;, 0666);

system('cls');
foreach $name(keys %CUSTLIST1){
$when = $CUSTLIST1{$name};
print &quot;\n\t$name $when\n&quot;;
}
print &quot;\n\tList complete. Press any key to continue.\n\t&quot;;
$answer = <STDIN>;
chomp($answer);


dbmclose(CUSTLIST1);
system('cls');
next;
}


 
$CUSTLIST1{$name, $phonenum} = $email;

I think this might be your problem. I'm not sure if the left side of the = is supposed to be a multidemnsional hash, but it doesn't make much sense as it is. If you want a 2D hash, then the format is $CUSTLIST1{$name}{$phonenumb}.

What you have there is a hash slice, which doesn't make much sense in the context used. To use a hash slice correctly, you might say:

($currentname,$currentphonenum) = $CUSTLIST1{$name,$phonenum}

In this, $name and $phonenum should be different keys in the hash.
 
I get errors when I do that, it says HASH then the memory address.

 
For a full explanation, see:

The wierd character is the subscript separator for multidimensional array emulation:
Code:
$SUBSCRIPT_SEPARATOR  # with use English
$SUBSEP               # with use English 
$;
Which will appear with the code snippet:
Code:
foreach $name(keys %CUSTLIST1){
    $when = $CUSTLIST1{$name};
    print &quot;\n\t$name  $when\n&quot;;
}
A simple fix would be:
Code:
foreach $name(keys %CUSTLIST1){
    $when = $CUSTLIST1{$name};
    $name =~ s/$;/,/g;  # replace subsep with comma
    print &quot;\n\t$name  $when\n&quot;;
}
Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top