Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Help with an array Conversion

by barrycarlyon (Beadle)
on Apr 26, 2006 at 21:19 UTC ( #545881=perlquestion: print w/replies, xml ) Need Help??
barrycarlyon has asked for the wisdom of the Perl Monks concerning the following question:

In the node Help with an IF?, I was told about this:

my $title = 'Political Animal'; # Or in your case, $q->param('title') my %links_to = ( "Political Animal" => "pa", "Newslink" => "nl", "New Slang" => "ns", "Rant With Ryan" => "rwr", "Planet Venus" => "pv", "All Day Breakfast" => "adb", ); my $link = $links_to{$title};

Upon trying something similar but with:

my %DAYS_OF_WEEK = ("0" => "Sunday", "1" => "Monday", "2" => "Tuesday", "3" => "Wednesday", "4" => "Thursday", "5" => "Friday", "6" => "Saturday", ); my $dayname = $DAYS_OF_WEEK($day);

Where $day is obtained using Class::Date (well locatine time actually)

It generates a 500 Internal Server Error, what am I doing wrong?

Yours Hopefully

Barry Carlyon

2006-04-27 Retitled by g0n, as per Monastery guidelines
Original title: 'SOLVED Help with an array Conversion'

Replies are listed 'Best First'.
Re: Help with an array Conversion
by Paladin (Priest) on Apr 26, 2006 at 21:31 UTC
    Hashes are indexed with { } not ( ) like you have.
Re: Help with an array Conversion
by Zaxo (Archbishop) on Apr 26, 2006 at 21:32 UTC

    What message do you get in your server error log?

    What you show will work if used properly [Oops, not quite - Paladin++ caught a true error]. You're not showing exactly how you get $day or what happens if a value for an undefined key is read from the hash.

    With a numerical sequence of keys, an array may be more natural than a hash for that. One of the many time string utilities may be better yet. I like,

    use POSIX 'strftime'; my $dayname = strftime '%A', localtime;
    That will honor locale, so anybody can run your script and get the day named in their own language.

    After Compline,

      my (undef, undef, undef, undef, undef, undef, $day, undef, undef) = localtime(time);

      And the error log: still searching the error log,

      changed () to {} still an error

      error log:

      Wed Apr 26 21:40:03 2006 alert client /home/lsrfm/webs/ Global symbol "$DAYS_OF_WEEK" requires explicit package name at /home/lsrfm/webs/ line 271. /weblogs/lon-web-1/httpd-error.log:syntax error at /home/lsrfm/webs/ line 271, near "$DAYS_OF_WEEK(" /weblogs/lon-web-1/httpd-error.log:BEGIN failed--compilation aborted at /home/lsrfm/webs/ line 5. /weblogs/lon-web-1/httpd-error.log:Compilation failed in require at /home/lsrfm/webs/ line 8. /weblogs/lon-web-1/httpd-error.log:BEGIN failed--compilation aborted at /home/lsrfm/webs/ line 8.

      Barry Carlyon

        Ah, very good, you're using strict. You need to declare my $DAYS_OF_WEEK; or use vars $DAYS_OF_WEEK; somewhere at or before first using the name. The error Paladin caught will then crop up unless you've already fixed it.

        After Compline,

Re: Help with an array Conversion
by ikegami (Pope) on Apr 26, 2006 at 22:06 UTC

    Why not use an array?

    my @DAYS_OF_WEEK = qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday ); my $dayname = $DAYS_OF_WEEK[$day];

    That said, POSIX's strftime is indeed even better.

      Solved Please Ignore (i looked at cpan docs, lol)

      Given posix strftime is there a similar way to obtain the month name?

      Barry Carlyon
        >perl -e "use POSIX; print strftime '%b', localtime Apr >perl -e "use POSIX; print strftime '%B', localtime April
Re: Help with an array Conversion
by perladdict (Chaplain) on Apr 27, 2006 at 04:50 UTC

    Here is my code.

    #!/usr/bin/perl -w my $day='0'; my %DAYS_OF_WEEK = ("0" => "Sunday", "1" => "Monday", "2" => "Tuesday", "3" => "Wednesday", "4" => "Thursday", "5" => "Friday", "6" => "Saturday", ); my $dayname = $DAYS_OF_WEEK{$day}; print "$dayname\n";

    Edit: g0n corrected code tags

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://545881]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2017-04-25 22:58 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (466 votes). Check out past polls.