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

craziness:

%hash = ( one => [1,2,3,4,5], two => [a,b,c,d,e], the => [l,m,n,o,p] ); foreach my $key ( keys %hash ) { push @cols, $key; push @vals, scalar( join ",", @{$hash{$key}} ); } foreach $val ( @vals ) { print "$val"; print "\n"; } print "\n"; $statement = 'insert into this (' . (join ",", @cols) . ') values (' . + (join ",", @vals) . ')'; print "$statement\n\n";
prints:
/home/jptxs > perl sortHashForInsert l,p a,b,c,d,e 1,2,3,4,5
yet change the 'the' hash key's value to anything else and:
/home/jptxs > perl sortHashForInsert 9,8,7,6,5 a,b,c,d,e 1,2,3,4,5
craziness!

-- I'm a solipsist, and so is everyone else. (think about it)

Replies are listed 'Best First'.
works now under strict : )
by jptxs (Curate) on Oct 11, 2000 at 08:02 UTC

    got it to work with help from the Chatter crew, but the errors it spit out at first were:

    Bareword "a" not allowed while "strict subs" in use at sortHashForInse +rt line 5. Bareword "b" not allowed while "strict subs" in use at sortHashForInse +rt line 5. Bareword "c" not allowed while "strict subs" in use at sortHashForInse +rt line 5. Bareword "d" not allowed while "strict subs" in use at sortHashForInse +rt line 5. Bareword "e" not allowed while "strict subs" in use at sortHashForInse +rt line 5. Bareword "l" not allowed while "strict subs" in use at sortHashForInse +rt line 6. Bareword "p" not allowed while "strict subs" in use at sortHashForInse +rt line 6.
    notice it only complains about l and p, the first and last elements of the list on line 6...why??

    because m,n,o, gets interpreted as m/n/o/ the commas get used as alternative quotes for the match operator 'm'
    Perl is too gracious : )

    use strict; my %hash = ( one => [qw(1 2 3 4 5)], two => [qw(a b c d e)], the => [qw(l m n o p)] ); my (@cols, @vals); foreach my $key ( keys %hash ) { push @cols, $key; push @vals, scalar( join ",", @{$hash{$key}} ); } foreach my $val ( @vals ) { print "$val"; print "\n"; } print "\n"; my $statement = 'insert into this (' . (join ",", @cols) . ') values ( +' . (join ",", @vals) . ')'; print "$statement\n\n";

    -- I'm a solipsist, and so is everyone else. (think about it)

Re: l
by japhy (Canon) on Oct 11, 2000 at 08:40 UTC
    This is the same reason that CGI.pm offers Tr() and TR() in place of tr() for the table row HTML element -- tr is an operator that can't be overloaded. Same with the s HTML tag (for striking through text). And if they ever come out with a y or m tag, they'll have to be called as Y() and M().

    $_="goto+F.print+chop;\n=yhpaj";F1:eval
Re: l
by merlyn (Sage) on Oct 11, 2000 at 08:06 UTC
Re: l
by Anonymous Monk on Oct 11, 2000 at 19:48 UTC
    These are exactly equivalent:
    the => [ l, m,n,o, p ] the => ['l', ($_ =~ m/n/o), 'p']
    Since the middle term in there, using the match operator, evaluates to undef, that term in the list is ignored entirely, giving you a list reference with two elements: l and p
      err.. by "list reference" I meant "array reference".. and by "anonymous monk" I meant "Fastolfe".