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:
If statement

 



kyfellar
Deleted

Apr 22, 2001, 4:09 PM

Post #1 of 2 (116 views)
If statement Can't Post

I have an if statement the is being used to return either a 0 or -1 back to my main program from a subroutine in my module. When I enter 111111111 (9 1's) it appears that it found a match when there is no record that matches that. Here is my code:

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


Any ideas why?





freddo
User

Apr 22, 2001, 9:16 PM

Post #2 of 2 (110 views)
Re: If statement [In reply to] Can't Post

Re Kyfellar,

Your code doesnt work, a little error sneaked in, you forgot the shift, to get the good value in $del, you should replace $del = @_; with $del = shift @_;, so that the following:

Code
sub delrecord { 
$del = shift @_;
if ($records{$del}) {
delete $records{$del};
$rc = 0;
} else {
$rc = -1;
}
return $rc;
}

$records{"hello"}="world";
print delrecord("hello"),"\n";
print delrecord("111111111"),"\n";

will output:

Code
administrator@COKE /cygdrive/h/scripts.pl/perlguru 
$ perl ky2.pl
0
-1

i optimized this a little with:

Code
sub delrecord { 
return 0 if (delete $records{shift @_});
return -1;
}

$records{"hello"}="world";
print delrecord("hello"),"\n";
print delrecord("111111111"),"\n";

if you need more details on how this works, just ask me :-)

(and i still dont see the point for all that 0/-1 stuff, perl handles it more efficiently with delete but if someone asked you...)

i hope this helps
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