If this is what you want to do then you could do any of these:

for my $i ( 0..11 ) { print "yes it's a month\n" if $month eq $monthnames[$i]; } for my $valid_months ( @monthnames ) { print "yes it's a month\n" if $month eq $valid_months; } print "Yes it's a month\n" if grep { $_ eq $month } @monthnames;

You would be better to use a hash as a lookup table like this:

my @months = qw( jan feb mar apr may jun jul aug sep oct nov dec); my %monthnames; # create lookup table $monthnames{$_}++ for @months; print "Yes it's a month!" if exists $monthnames{$month};

Warning

In your if($month = $array[i]){ statement you have used = which is the assignment operator when what you really wanted was eq the string equality operator. In perl we have:

$a = 1; # set $a to 1 $a == 1; # returns a true value if $a is numerically equal to 1 $a eq 'foo' # returns a true value if $a is equal to the string value + 'foo'

Using = when you mean == or eq can be very confusing. The assignmet will always succeed so the conditional statement is generally also always true so you have a nasty bug that not only fails to work as expected but also modifies your data. You also need to make sure you use eq rather than == when you want a string comparison:

print "What, a string is numerically == 0?" if 'foo' == 0;
cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Re: Re: creating an array of names by tachyon
in thread creating an array of names by bdawg613

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.