On Sat, Jun 30, 2001 at 12:28:11AM +0200, M.W. Koskamp wrote:
> From: Wang, Lanbo <[EMAIL PROTECTED]>
> > Hi Members,
> > #!/usr/local/bin/perl
> >
> > sub card{
> >
> >   my %card_map;
> >   my ($num)=@_;
> >  @card_map{1..9}= qw(one two three four five six seven eight nine);
> 
> You are putting the numbers 0..9 in the array @card_map here, not in the
> hash %cardmap.
> Those are different things.
> You should acces the array instead.

@card_map{1..9} = (...) is a hash slice assignment; it does indeed
initialize %card_map, not @card_map.

It's a good point that he should be using an array, though.


> Another top is to declare and fill @card_map outside the function.
> It only needs to be filled once.

Another good point.


> Tru this:
> 
> #!/usr/local/bin/perl
> my  @card_map{1..9}= qw(one two three four five six seven eight nine);

Wrong syntax here, though, for various reasons.  You wanted
  my @card_map;
  @card_map[1..9] = qw(...);

You can't declare a hash or array slice, so the declaration has to be
seperate.  Also, of course, @card_map{1..9} is still a hash slice.


> sub card{
>    return $card_map[$_[0]] || $_[0];
> }
> 
> while (<>) {
>   chomp
>   print "card  of $_ is:" &card($_), "\n";
> }

You missed the mistake too; the missing comma after print's first argument
is the problem, everything else he has is, technically, fine.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

Reply via email to