Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Critique

by davido (Archbishop)
on Dec 06, 2003 at 06:08 UTC ( #312735=note: print w/replies, xml ) Need Help??

in reply to Critique

Here are a few snippets you can use as examples for "shortening" your code. Whether it improves your code is something you'll have to decide for yourself. Golfed down code isn't necessary more legible or readable.

#hash slice with a range my %months; @months{1..12} = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;

You could write your "get_days()" function like this:

sub get_days { my $month = shift; return 31 if grep { $month eq $_ } qw/1 3 5 7 8 10 12/; return 30 if grep { $month eq $_ } qw/4 6 9 11/; return 29 if $month eq '2'; die "Bad month entered!\n"; }

This method eliminates the use of global variables that intentionally leak into functions.

For more speed-efficient lookup tables hashes are preferable, but these are small lists, and you're probably not checking thousands of times a second, so grep is probably fine. Not sure why I chose to treat month numbers as strings. I guess because I usually think of hash keys as strings too.

Those are the biggest changes I can think of, and they're really not necessary. Just look at them as "Another Way To Do It"


Replies are listed 'Best First'.
Re: Re: Critique
by phenom (Chaplain) on Dec 06, 2003 at 14:43 UTC
    I've never seen this before:
    my %months; @months{1..12} = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
    Thanks, I like it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://312735]
[LanX]: good morning
[choroba]: LanX good indeed
[choroba]: very cloudy here, I planned to make some picrures, but now I don't think so
[LanX]: do you like it better now?
[choroba]: Well, getting used to it
[LanX]: several people told me they like saigon. ..

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2017-02-26 04:29 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (371 votes). Check out past polls.