#!/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";
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
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.
[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 );
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.
⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊
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.
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