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

You've done well so far. You'll need two if{} statements inside your loop though. One to check if there is a match and one to warn when the user has chosen LA.

Your if statement should check against the loop variable $city instead of your global $myCities.

Normally for lookups I prefer to store the items in a hash. Then you don't need a loop. You just say something like "if ( exists $hash-of-cities{ $input_city }){do-something}". To handle partial inputs you store only the first three letters of the city and match those against the first three letters of the user's input.

A nice bonus add-in would be to store both the city name and it's abbreviation in the hash like this: %cities=( 'New'=>'1', 'NY'=>"1", .... ); then doing a hash lookup with either input will be successful.

Good luck. Keep at it!

#!/usr/bin/perl # # cities - lookup cities in a list, take partial inputs # use warnings; use strict; my $myCities = "Baltimore:Chicago:Los Angeles:New York:San Diego:"; my @myCities = split(':', $myCities); my $warning = "I'm sorry,we no longer tour LA, please try again."; my $prompt = <<EOF; Please pick one of the following cities by entering any portion of the beginning of the city name: (Baltimore Chicago Los Angeles New York San Diego) EOF print "$prompt\n>"; while ( my $input_city = <STDIN> ) { chomp $input_city; print "$warning\n" if ( $input_city eq "Los Angeles" ); foreach my $city ( @myCities) { print "Found $input_city\n" if ( $input_city eq $city ); } print ">"; }

s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
  • Comment on Re: **HomeWork** Trying to search an array and match it to a user input
  • Download Code