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}

In reply to Re: **HomeWork** Trying to search an array and match it to a user input by starbolin
in thread **HomeWork** Trying to search an array and match it to a user input by trenchwar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.