sim has asked for the wisdom of the Perl Monks concerning the following question:

Hey I am trying to just write first simple perl script. Here what I am trying to do is pass the word which I want to find in array from command line. After that it should print all entries which contain that word. Say I want to find Jacob from command line but its not returning any result. Could u help me out

#! usr/bin/perl use Fcntl; print “content-type: text/html \n\n”; print “Enter word : “; $word = ; @myNames = (‘Jacob’, ‘Michael’, ‘Joshua’, ‘Matthew’, ‘Alexander’, ‘And +rew’); @grepNames = grep(/$word/, @myNames); foreach $Names(@grepNames) { print $Names; print “\n”; }

Replies are listed 'Best First'.
Re: find words in array
by toolic (Bishop) on Aug 01, 2012 at 18:15 UTC
Re: find words in array
by thundergnat (Deacon) on Aug 01, 2012 at 18:34 UTC

    Also, be careful of your quoting characters. “ and ” are not the same as " and ". Same thing with ‘ and ’ instead of ' and '. Some "smart editors" may change them automatically. Make sure you are not being sabotaged by your text editor.

Re: find words in array
by Rudolf (Pilgrim) on Aug 01, 2012 at 18:42 UTC

    Here is an example using pattern matching, if your looking to see if the word is contained within any elements of the array

    use v5.14; chomp(my $word = <STDIN>); #get word from command line #chomp removes newline for search foreach my $name(@array){ #loop through names if($name =~ m/$word/i){ #if the word is contained within say $name; # print out the name with a newline (say) } }

    otherwise, if your looking for exact maches just do something like this.

    use v5.14; chomp(my $word = <STDIN>); foreach my $name(@array){ if($name eq $word){ #if the name equals the word say $name; } }
Re: find words in array
by Kenosis (Priest) on Aug 01, 2012 at 20:03 UTC

    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!