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:
Deleting a record in a hash

 



kyfellar
Deleted

Apr 21, 2001, 5:14 AM

Post #1 of 5 (394 views)
Deleting a record in a hash Can't Post

I am trying to delete a record from a has based on matching the key. The key is inputed from the user in the main program and then passed to a function in my module. I have code that looks similar to this:

sub delrecord
{
my ($del) = @_;
if ($records{$del} =~ /.*/)
{
delete $records{$del};
$rc = 0;
}
else
{
$rc = -1;
}
return ($rc);
}

%records is defined and loaded prior to this function and the function should return 0 if it matches a record and -1 if there is no match.

Can you tell me what is wrong here?



zanardi
journeyman

Apr 21, 2001, 8:56 PM

Post #2 of 5 (386 views)
Re: Deleting a record in a hash [In reply to] Can't Post


Code
if ($records{$del} =~ /.*/)

no need for a regular expression on that


Code
if ($records{$del})

that is all you need

----------------------
Fueled By ZCom


freddo
User

Apr 22, 2001, 7:48 AM

Post #3 of 5 (376 views)
Re: Deleting a record in a hash [In reply to] Can't Post

Hello Kyefellar,

Zanardy is right, i dont understand why you would need the =~ /.*/. Btw for this case, you should better use if (exists $records{$del}) instead.

note that i dont see the point to make a subroutine like that one, you should use something like:

Code
$records{"hello"}="world"; 

$del = "hello";
if (delete $records{$del})
{ print "deleted $_\n"; } else { print "gone bad\n"; }

$del = "will fail";
if (delete $records{$del})
{ print "deleted $_\n"; } else { print "gone bad\n"; }

that will output:

Code
administrator@COKE /cygdrive/h/scripts.pl/perlguru 
$ perl kyefellar.pl
deleted
gone bad

i recommand you to read:
What's the difference between "delete" and "undef" with hashes? in perldoc perlfaq4, exists in perldoc perlfunc, delete in perldoc perlfunc, and the excellent article Autovivification : What Is It and Why Do I Care? from Uri Guttman

i hope this helps
freddo

;---
Real Programmers are surprised when their cars'odometer don't turn
from 99999 to 9999A!?


kyfellar
Deleted

Apr 22, 2001, 4:58 PM

Post #4 of 5 (370 views)
Re: Deleting a record in a hash [In reply to] Can't Post

The reason for the seperate subroutine is because the person wanting the program is wanting this portion in a seperate subroutine. I definitely understand your point though.

Can you answer this for me? When I type the value 111111111 in as the rcord to be searched for and deleted, it returns 0 saying that it found it, but this record does not actually exist. If I key it in again, it says that it does not exist. Why is that?



freddo
User

Apr 22, 2001, 8:55 PM

Post #5 of 5 (364 views)
Re: Deleting a record in a hash [In reply to] Can't Post

Hi Kyfellar,

nope i really dont understand why, may be you're doing something wrong, could you paste your code here?

here's my try at it:

Code
administrator@COKE ~ 
$ perl -de 1
Default die handler restored.
Loading DB routines from perl5db.pl version 1.07
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1): 1
DB<1> print "shouldnt print this" if (exists $record{111111111})
DB<2> $record{"hello"}="world"
DB<3> print "shouldnt print this" if (exists $record{111111111})
DB<4> print "shouldnt print this" if (exists $record{"111111111"})
DB<5> if (delete $records{"111111111"}) { print "deleted $_\n"; } else { print "gone bad\n"; }
gone bad
DB<6> q

i really dont understand how you could get the result you talk about?

freddo

;---
Real Programmers are surprised when their cars'odometer don't turn
from 99999 to 9999A!?

 
 


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

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