awohld has asked for the wisdom of the Perl Monks concerning the following question:

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->{ $_ } } @intersectio +n; } my %intersection; @intersection{ @intersection } = (); return \%intersection; } my $newIntersection = &intersection( \%chicago, \%wisconsin, \%rockfor +d ); print join(" ", keys %{ $newIntersection }), "\n";

Replies are listed 'Best First'.
Re: Syntax Error using STRICT and Declaring Variable
by ikegami (Patriarch) on Feb 02, 2006 at 04:45 UTC

    my's argument must be a variable name, or a list of variables in parens.
    @chicago{   qw(tiger bob munch toy)    }
    is neither. 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 );

      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
by monarch (Priest) on Feb 02, 2006 at 04:49 UTC
    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
by Cody Pendant (Prior) on Feb 02, 2006 at 04:45 UTC
    ..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
      He's trying to create a hash, and initialize it using a hash slice at the same time.

      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.

      I got it straight out of the "Perl Cookbook".
        I bet there was no my.
Re: Syntax Error using STRICT and Declaring Variable
by tweetiepooh (Hermit) on Feb 02, 2006 at 11:56 UTC
    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.

    The hash reference define I found somewhere else on this site but I can't remember where.