|
Prev: use of DBI; I am getting multiple error messages mixed in with?the correct output.
Next: FAQ 2.5 I grabbed the sources and tried to compile but gdbm/dynamic loading/malloc/linking/... failed. How do I make it work?
From: Ben Morrow on 24 Apr 2008 23:49 Quoth "A. Sinan Unur" <1usa(a)llenroc.ude.invalid>: > ds456 <dsxxxxx(a)yahoo.com> wrote in > news:SPWdnfNw-qCDv4zVnZ2dnUVZ_jidnZ2d(a)oco.net: > > > To put it logically, rather than in code, I am trying to do this... > > > > $wx->$widget( > > foreach $key (keys %hash){ > > $key => $hash{$key} , > > } > > )->place(-x => somexpos, -y => someypos); > > I am afraid your logic does not seem suitably matched to Perl's > semantics. You cannot embed a for loop in an argument list. > > $wx->$widget( %hash )->place( ... ); You can. It's called 'map': $wx->widget( map { $_ => $hash{$key} } keys %hash )->place(...); The fact that that particular map statement is equivalent to simply evaluating the hash in list context doesn't mean the construction isn't useful in general. Ben -- don't get my sympathy hanging out the 15th floor. you've changed the locks 3 times, he still comes reeling though the door, and soon he'll get to you, teach you how to get to purest hell. you do it to yourself and that's what really hurts is you do it to yourself just you, you and noone else ** ben(a)morrow.me.uk
From: A. Sinan Unur on 25 Apr 2008 00:16
Ben Morrow <ben(a)morrow.me.uk> wrote in news:ju08e5-prp1.ln1(a)osiris.mauzo.dyndns.org: > > Quoth "A. Sinan Unur" <1usa(a)llenroc.ude.invalid>: >> ds456 <dsxxxxx(a)yahoo.com> wrote in >> news:SPWdnfNw-qCDv4zVnZ2dnUVZ_jidnZ2d(a)oco.net: >> >> > To put it logically, rather than in code, I am trying to do >> > this... >> > >> > $wx->$widget( >> > foreach $key (keys %hash){ >> > $key => $hash{$key} , >> > } >> > )->place(-x => somexpos, -y => someypos); >> >> I am afraid your logic does not seem suitably matched to Perl's >> semantics. You cannot embed a for loop in an argument list. >> >> $wx->$widget( %hash )->place( ... ); > > You can. It's called 'map': No, a for loop and map are different beasts. The fact that map is a one- to-possibly many transformation applied to the argument list and each invocation to map could be translated into a loop equivalent and vice versa does not make map and for equivalent. > > $wx->widget( > map { $_ => $hash{$key} } keys %hash > )->place(...); > > The fact that that particular map statement is equivalent to simply > evaluating the hash in list context doesn't mean the construction > isn't useful in general. Of course, map is useful. However, the OP was transforming the whole hash into a single string that looked like how a method call would look like in the source and was surprised when that did not do what he thought it would do. Clearly, there is a mismatch between how the OP understands Perl constructs and what they mean. What is the point of throwing the identity transformation into the argument list? After all, I could also do the following: #!/usr/bin/perl use strict; use warnings; my %hash = qw( a 1 b 2 c 3 ); yabadabadooo( do { my @args; for my $k ( sort keys %hash ) { push @args, $k, $hash{$k}; } @args; } ); sub yabadabadooo { print "@_\n"; } __END__ and thereby place a for loop in the function call but that is just as irrelvant as your response. Sinan -- A. Sinan Unur <1usa(a)llenroc.ude.invalid> (remove .invalid and reverse each component for email address) comp.lang.perl.misc guidelines on the WWW: http://www.rehabitation.com/clpmisc/ |