Syntax Error using STRICT and Declaring Variable
awohld
created: 2006-02-01 23:33:35
Why does this throw a syntax error when declaring the @chicago, @wisconsin, and @rockford variables?
#!/usr/bin/perl -w

use strict;

my @chicago{   qw(tiger bob munch toy)    } = ();
my @wisconsin{ qw( tiger sara munch toy ) } = ();
my @rockford{  qw( tiger sara love toy )  } = ();


sub intersection {
    my ( $i, $sizei ) = ( 0, scalar keys %{ $_[0] });
    my ( $j, $sizej );

    for ( $j = 1; $j < @_; $j++ ) {
           $sizej = keys %{ $_[ $j ] };
           ( $i, $sizei ) = ( $j, $sizej ) if $sizej < $sizei;
    }

    my @intersection = keys %{ splice @_, $i, 1 };
    my $set;
    while ( $set = shift ) {
             @intersection = grep { exists $set->{ $_ } } @intersection;
    }

    my %intersection;
    @intersection{ @intersection } = ();

    return \%intersection;
}

my $newIntersection = &intersection( \%chicago, \%wisconsin, \%rockford );

print join(" ", keys %{ $newIntersection }), "\n";
Re: Syntax Error using STRICT and Declaring Variable
created: 2006-02-01 23:45:25
..because that's not how you declare variables? Seriously, it's hard to know what you wanted to do, but my @chicago{ qw(tiger bob munch toy) } = () is pretty much totally broken from the very first "{".

Were you trying to create an array? A hash? An array within a hash? I honestly don't know.



($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
=~y~b-v~a-z~s; print
Re^2: Syntax Error using STRICT and Declaring Variable
created: 2006-02-01 23:48:36
He's trying to create a hash, and initialize it using a hash slice at the same time.
Re^2: Syntax Error using STRICT and Declaring Variable
created: 2006-02-01 23:48:58
I got it straight out of the "Perl Cookbook".
Re^3: Syntax Error using STRICT and Declaring Variable
created: 2006-02-01 23:50:03
I bet there was no my.
Re^2: Syntax Error using STRICT and Declaring Variable
created: 2006-02-02 06:15:12

More simply...

"...because you can't declare a hash slice. Only a hash as a whole."

the kind of dwimmery which he was expecting, which is to condense a declaration and the assignment of a slice in a single statement does not exist - and indeed would better not exist, for as far as I can say, it's not consistent at all.

Re: Syntax Error using STRICT and Declaring Variable
created: 2006-02-01 23:45:28

[doc://my|my]'s argument must be a variable name.
@chicago{ qw(tiger bob munch toy) }
is not a variable name. I'm afraid you'll have to do

my %chicago;   @chicago{   qw( tiger bob munch toy  ) } = ();
my %wisconsin; @wisconsin{ qw( tiger sara munch toy ) } = ();
my %rockford;  @rockford{  qw( tiger sara love toy  ) } = ();

or

my %chicago   = map { $_ => undef } qw( tiger bob munch toy  );
my %wisconsin = map { $_ => undef } qw( tiger sara munch toy );
my %rockford  = map { $_ => undef } qw( tiger sara love toy  );

or even better (since you won't have to use exists)

my %chicago   = map { $_ => 1 } qw( tiger bob munch toy  );
my %wisconsin = map { $_ => 1 } qw( tiger sara munch toy );
my %rockford  = map { $_ => 1 } qw( tiger sara love toy  );
Re^2: Syntax Error using STRICT and Declaring Variable
created: 2006-02-02 02:13:32

Double-plus ungood for even suggesting those map solutions. Gawd. This is TIMTOWDI taken far too far. The first suggestion was the best by far.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Syntax Error using STRICT and Declaring Variable
created: 2006-02-01 23:49:18
Altering the three assignment lines to:
 my %chicago = map { $_ => 1 } qw(tiger bob munch toy);
 my %wisconsin = map { $_ => 1 } qw( tiger sara munch toy );
 my %rockford = map { $_ => 1 } qw( tiger sara love toy );
gives you the following output:
 toy tiger

The map command iterates over each element in the array (e.g. ('tiger','bob','munch','toy')) and assigns a key with the name of that element and a value of 1.

Re: Syntax Error using STRICT and Declaring Variable
created: 2006-02-02 06:56:56
It may not be quite what's wanted but if you use
my $chicago = { qw(tiger bob munch toy) };
my $wisconsin = { qw(tiger sara munch toy) };
my $rockford = { qw(tiger sara love toy) };
...
my $newIntersection = &intersection($chicago,$wisconsin,$rockford);

OUTPUT
tiger - only common key in all 3 hashes

Also 
my $chicago = { qw(tiger 0 bob 0 munch 0 toy 0 ) };
my $wisconsin = { qw(tiger 0 sara 0 munch 0 toy 0 ) };
my $rockford = { qw(tiger 0 sara 0 love 0 toy 0 ) };

OUTPUT
tiger toy - common keys in all 3 new hashes.
We don't keep the values though. key => undef.

perlmonks.org content © perlmonks.org and awohld, blazar, Cody Pendant, diotalevi, ikegami, monarch, tweetiepooh

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03