Stuart White <[EMAIL PROTECTED]> wrote:
: 
: sub ParseLineForHomeAndVisitors()
: {
:  if ($_
: =~/(Spurs|Suns|Mavericks|Lakers|Clippers|Cavaliers|
: Celtics|Pacers|Pistons|Wizards|Warriors|Bulls|Hawks|
: Raptors|Magic|Heat|Kings|Rockets|Nuggets|Grizzlies|
: Jazz|Knicks|Nets|Supersonics|'Trail Blazers'|Bucks|
: Timberwolves|Hornets|Sixers|Bobcats)/)
:  {
:   @teams = /([[:alpha:]]+)/g;
:  }
:  return (@teams);
: }

    When writing a subroutine, think of it as a miniature
program. Something is passed in, processed, and output. Some
subs only do one or two of those, but many do all three. Try
not to process anything that is not local to the sub. In
ParseLineForHomeAndVisitors(), $_ is not local to the sub,
neither is @teams. It would be better to pass that value in:

sub ParseLineForHomeAndVisitors {
    my $line  = shift;
    my @teams = ( $line =~ /([[:alpha:]]+)/g );
    return @teams;
}

    I left out the validation because the sub isn't named
ParseLineForHomeAndVisitorsIfLineIsValid() and because, IMO,
that's too much work for one sub. For the validation I'll
give a hint: Don't use a regex. Read three slides from
M-J Dominus:

 http://perl.plover.com/yak/hw2/samples/slide007.html


: I tried the same thing with my hash:
: 
: CreateAbbrevAndNicknamesHash();
: 
: and here's the sub:
: sub CreateAbbrevAndNicknamesHash()
: {
: my %AbbrevAndNicknames;
: %AbbrevAndNicknames = (
:        IND=>  "Pacers",
:               .
:               .
:               .
:       );                                                      
:                                                               
:                       
: }

    The big question here is: Why a subroutine?
If you want to create a hash, create a hash:

my %AbbrevAndNicknames = (
        IND => 'Pacers',
        NJN => 'Nets',
        DET => 'Pistons',
        NOH => 'Hornets', #check this
        MIL => 'Bucks',
        CLE => 'Cavaliers',
        BOS => 'Celtics',
        NYK => 'Knicks',
        MIA => 'Heat',
        PHI => 'Sixers',
        TOR => 'Raptors',
        ATL => 'Hawks',
        WAS => 'Wizards',
        ORL => 'Magic',
        CHI => 'Bulls',
        CHA => 'Bobcats',
        SAC => 'Kings',
        MIN => 'Timberwolves',
        SAN => 'Spurs',
        LAL => 'Lakers',
        DAL => 'Mavericks',
        MEM => 'Grizzlies',
        HOU => 'Rockets',
        DEN => 'Nuggets',
        UTA => 'Jazz',
        POR => 'Trail Blazers',
        SEA => 'Supersonics',
        LAC => 'Clippers',
        GSW => 'Warriors',
        PHX => 'Suns',
);

    I don't see the point of the subroutine. If
you want to return a hash from a subroutine,
use 'return'. Note that foo() actually returns
a list (or is it an array?). We decide to stuff
the result into a hash.


#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper 'Dumper';

my %foo_hash = foo();

print Dumper \%foo_hash;

sub foo {
    return (
        SEA => 'Supersonics',
        LAC => 'Clippers',
        GSW => 'Warriors',
        PHX => 'Suns',
    );
}


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328








-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to