Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: **HomeWork** Trying to search an array and match it to a user input

by NetWallah (Canon)
on Mar 27, 2008 at 17:43 UTC ( #676810=note: print w/replies, xml ) Need Help??


in reply to **HomeWork** Trying to search an array and match it to a user input

A few more points:
  • Comparing $input_city with $myCities will always fail (Unless someone typed the entire city list). You probably intended to compare with $city
  • With your current code, every failed comparison will print the "I'm sorry" message. That should only print if ALL comparisons fail (as in after the for loop.)
  • As pointed out earlier, "==" is for numeric comparison. Try "eq".
  • The uc and lc functions can be used to make the comparisons case insensitive (or you could use a regular expression).
If you have studied hashes, using that construct would make your code considerably simpler, and easier to read.

Good luck.

     "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

  • Comment on Re: **HomeWork** Trying to search an array and match it to a user input

Replies are listed 'Best First'.
Re^2: **HomeWork** Trying to search an array and match it to a user input
by Arif (Acolyte) on Mar 27, 2008 at 18:06 UTC
    You can also take a look at the module Text::Abbrev. It makes it easy to work with abbreviations of strings.
    %hash = abbrev (split(':', "Baltimore:Chicago:Los Angeles:New York:San Diego:"));
    chomp (my $input_city = <STDIN> );
    if (exists $hash{$input_city}) { ...... }
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://676810]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2021-10-19 03:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (76 votes). Check out past polls.

    Notices?