473,394 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

code is not working

blackgoat
Hi!

Pls help! My code is not working! Pls tell me whats wrong with it... it returns value 1 in all cases!!

Expand|Select|Wrap|Line Numbers
  1. %dbase = {
  2.         apple => "fruit",
  3.         brinjal => "vegetable" ,
  4.         coffee => "beverage",
  5.         };
  6.  
  7. my @info;
  8.  
  9. $info[0]="lemon";
  10. $info[1]="juice";
  11.  
  12. $result = validate($info[0],$info[1]);
  13.  
  14. sub validate
  15.      {
  16.         my $usr = @_[0];
  17.         my $pwd = @_[1];
  18.  
  19.         while (($u, $p) = each %dbase)
  20.             {  
  21.               if ($usr == $u && $pwd == $p)
  22.                     { return(1);
  23.                       break;
  24.                     }
  25.               else
  26.                     { return(0); }
  27.             }
  28.      }    
  29.  
  30.  
  31. {print "$result"};
Thanks

BG
Mar 4 '10 #1
2 1318
nithinpes
410 Expert 256MB
There are multiple errors in your script.
1. the way you have defined hash
Expand|Select|Wrap|Line Numbers
  1. %dbase = { 
  2.         apple => "fruit", 
  3.         brinjal => "vegetable" , 
  4.         coffee => "beverage", 
  5.         }; 
  6.  
Using curly braces will create an anonymous hash(a hash reference). You should use parentheses in this case.

2. The return statement should be after the entire processing. Otherwise, in the first iteration itself it will come out of the subroutine.

3. Use "last" instead of "break".

The modified code that works is as below:
Expand|Select|Wrap|Line Numbers
  1. %dbase = (
  2.         apple => "fruit", 
  3.         brinjal => "vegetable" , 
  4.         coffee => "beverage", 
  5.         ); 
  6.  
  7. my @info; 
  8.  
  9. $info[0]="lemon"; 
  10. $info[1]="juice"; 
  11.  
  12. $result = validate($info[0],$info[1]); 
  13.  
  14. sub validate 
  15.      { 
  16.         my $usr = @_[0]; 
  17.         my $pwd = @_[1];         
  18.         my $r=0;
  19.         while (($u, $p) = each %dbase) 
  20.             {             
  21.               if ($usr eq $u && $pwd eq $p) 
  22.  
  23.                     {                       
  24.                       $r=1;
  25.                       last; 
  26.                     } 
  27.               else 
  28.                     { 
  29. $r=0; } 
  30.        }     
  31.   return $r     }     
  32.  
  33. print "$result\n"; 
  34.  
Mar 4 '10 #2
okay... Thanks a lot!
Mar 4 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
0
by: George2 | last post by:
Hello everyone, From the definition of working set, it is a subset of virtual pages resident in physical memory -- from book Windows Internals. It means working set could not be larger than...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.