I think you've done a good job with your first Perl script! The comments above on your coding are excellent.

As a suggestion, always begin you scripts with the following:

use strict; use warnings;

I tend to prefer this, instead, which contains the above:

use Modern::Perl;

In either case, these pragmas will catch problematic areas in your script, perhaps saving hours of debugging--among other things.

This being said, consider the following (which doesn't really do anything more than what's already been done above):

use Modern::Perl; my $word = 'a'; my @myNames = qw(Jacob Michael Joshua Matthew Alexander Andrew); if ( $word ~~ @myNames ) { # Using Perl's smart-matching operator say qq|Exact match of "$word" found.|; } elsif ( my @found = grep /\Q$word\E/i, @myNames ) { local $" = "\n"; # Printed between interpolated array's elements say qq|Substring/case-insensitive matches for "$word":\n@found|; } else { say qq|"$word" not found.|; }

Output:

Substring/case-insensitive matches for "a": Jacob Michael Joshua Matthew Alexander Andrew

Actually, the primary difference in the above script is its using Perl's 'smart' operator to check if an element exists in an array.

Hope this helps!


In reply to Re: find words in array by Kenosis
in thread find words in array by sim

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.