SPUG: re: 'my' question

Paul Farrall PFarrall at getthere.com
Thu May 18 17:54:14 CDT 2000


This works fine on my machine ( v5.6.0 built for sun4-solaris).
Perhaps there is something in the '...' section that makes 
@pairs not what you think it is?

Paul Farrall

---
#!/usr/local/bin/perl -w

use strict;
my @pairs = ( "a&b", "c&d", "e&f");

my %hash;
foreach my $pair( @pairs ){
    my($name, $value) = split(/&/, $pair );
    $hash{$name} = $value;
}


foreach (keys %hash) {
    print "$_: $hash{$_}\n";
}

---
$ ./s.pl 
a: b
c: d
e: f
---

-----Original Message-----
From: Alyssa Harding [mailto:axhard2 at uswest.com]
Sent: Thursday, May 18, 2000 1:40 PM
To: SPUG
Subject: SPUG: re: 'my' question


Okay, sorry, there was a typo in there, I was trying to split $pair,
not $buffer in the first foreach loop, the quoted text below is now
correct.

A.


Alyssa Harding wrote:
> 
> I encountered something wierd yesterday and I wonder if someone
> could explain why this happens.
> 
> I had a script which was doing something like this:
>   #!/usr/bin/perl -w
> 
>   ...
> 
>   foreach $pair (@pairs){
>     ($name, $value) = split(/&/, $pair);
>      $hash{$name} = $value;
>   }
> 
>   ...
>   This got me a nice hash with values and everything.
> 
> Okay, not too complicated or anything, but it wasn't using strict
> (and it had no reason for not doing so).  So I changed it to use
> strict;
> 
>   #!/usr/bin/perl -w
> 
>   use strict;
> 
>   ...
>   my %hash;
>   foreach my $pair( @pairs ){
>     my($name, $value) = split(/&/, $pair);
>     $hash{$name} = $value;
>   }
> 
> 
>   The resulting hash gets only one key/value pair - $pairs[0] - and
> nothing else.
>   Can someone please explain why this happens?  Shouldn't it work
> just as well?
> 
>   I changed it to:
> 
>   #!/usr/bin/perl
> 
>   use strict;
>   ...
>   my(%hash, $pair);
>   foreach $pair( @pairs ){
>    etc. etc.
>   }
> 
>   and it worked just fine.
> 
> A.
> --
> Alyssa W. Harding
> Web Programmer
> U S WEST Creative Services
> phon: (206) 345-4073
> cell: (206) 769-8735
> -------------------------------------------------------------------
> People who didn't need people needed people around to know that they
> were
> the kind of people who didn't need people.
>         -- (Terry Pratchett, Maskerade)
> 
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>      POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
>  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
>  For Subscriptions, Email to majordomo at pm.org:  ACTION  spug-list  EMAIL
>   Replace ACTION by subscribe or unsubscribe, EMAIL by your Email address

-- 
Alyssa W. Harding 
Web Programmer
U S WEST Creative Services
phon: (206) 345-4073
cell: (206) 769-8735
-------------------------------------------------------------------
Shadwell hated all southerners and, by inference, was standing at
the
North Pole.
        -- (Terry Pratchett & Neil Gaiman, Good Omens)

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 For Subscriptions, Email to majordomo at pm.org:  ACTION  spug-list  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email address


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 For Subscriptions, Email to majordomo at pm.org:  ACTION  spug-list  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email address





More information about the spug-list mailing list