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

Hi i have the following, which i find is tedious and fills up a lot of lines of my code:
$monthname[0] = 'jan'; $monthname[1] = 'feb'; . . . $monthname[11] = 'dec';
is there a way that i can do this shorter? i have some ideas, but i'm not sure how to implement it.
something like:
$monthname[] = {'jan', 'feb', ... ,'dec'};

Replies are listed 'Best First'.
Re: creating an array of names
by tachyon (Chancellor) on Nov 11, 2002 at 17:49 UTC
    @monthname = qw( jan feb mar apr may jun jul aug sep oct nov dec); print $monthname[$_], $/ for 0..11;

    cheers

    tachyon

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

Re: creating an array of names
by broquaint (Abbot) on Nov 11, 2002 at 17:49 UTC
    my @monthname = qw( jan feb mar apr may jun jul aug sept oct nov dec );
    See. perlop for more details.
    HTH

    _________
    broquaint

Re: creating an array of names
by LTjake (Prior) on Nov 11, 2002 at 17:50 UTC
    my @array = qw(jan feb .... dec);
    HTH

    Update: ECHO, Echo, echo, ec..... =)

    --
    Rock is dead. Long live paper and scissors!
      ok.. that's great. one more small question. to read through that array after. can i do:
      $i = 0; where 12 is the size of my array. and i is an index, or is there an al +ternative? while($i != 12){ if($month = $array[i]){ print "yes it's a month\n"; } }
      thank you soo much

        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

        See perldoc -f grep... My guess is that you want to compare the value in $month to a set of pre-defined values in your array.
        my @months=qw(jan feb mar); my $month="jan"; print "Found" if grep{$month eq $_}@months;
        You could also do:
        foreach $month (@array) { print "$month\n"; }
        print "yes it's a month\n" if grep ($_ eq $month, @array);
        The built-in grep function will return all the matches, and you care if the list is not empty.

        More generally, you could use a foreach loop to look at each element in turn.

        Use eq for string comparisons. = is assignment, == is numeric comparison.

        —John

        $i = 0; where 12 is the size of my array. and i is an index, or is there an al +ternative? while($i != 12){ if($month = $array[i]){ print "yes it's a month\n"; } }
        Well first I'll assume the $array[i] is a typo. second you don't seem to be reading through the array. $i is set to 0 and thats not 12 of course so you will loop through the while forever and depending on wether $month contains jan, since $i is the first element in the array it will print "yes it's a month\n" again,and again, and again......etc

        you could do it this way though
        foreach my $item ($array) { print "its the month of $month\n" if ($month eq $array[$i]; }
        I'm not sure if you're trying to find out if each element is a month or looking for one particular month. I just assumned you where looking for a particular month

        Update: replaced '=' with correct eq in if statement

        jjdraco
        learning Perl one statement at a time.
        ok.. that's great. one more small question. to read through that array after. can i do:
        $i = 0; where 12 is the size of my array. and i is an index, or is there an al +ternative? while($i != 12){ if($month = $array[i]){ print "yes it's a month\n"; } }
        Well there's one immediate error here, which is that you actually mean $montheq $array[i] as firstly these are strings and secondly you don't want to assign to $month!!

        However you would do better with code like this:

        for $i (0..$#array) { # just in case they change the number of months +:) next unless ($month eq $array[$i]); # skip if not equal print "Yes its a month\n"; last; # no need to check other months so exit loop }

        Dingus


        Enter any 47-digit prime number to continue.
Re: creating an array of names
by princepawn (Parson) on Nov 11, 2002 at 18:40 UTC
    Perhaps thou seekest Date::Ordinal

    Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

Re: creating an array of names
by Enlil (Parson) on Nov 11, 2002 at 17:52 UTC
    heres one solution:
    my @monthname = ('jan','feb','mar',...,'dec');
    so now:
    $monthname[0] = 'jan'; $monthname[1] = 'feb'; . . . $monthname[11] = 'dec';
    as requested.

    -enlil