RE: foreach ( hash element that maybe scalar, maybe array)

2004-11-02 Thread Ed Chester

Thanks for that Charles - 

 foreach my $bit ( values %bits ) {
if ( ref $bit eq 'ARRAY' ) {

yep, that helps a lot. I didn't know ref existed, very useful. I am still a bit
surprised there
isn't a way that perl can just infer what to do from context like it usually
does. Still, this
opens up ways to fix a couple of other outstanding issues I have as well. 

Thanks loads! 

ed c
 

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: foreach ( hash element that maybe scalar, maybe array)

2004-11-01 Thread Charles K. Clarkson
Ed Chester  wrote:

: Hi all.
: 
: I have a hash %BITS something like this:
: 
: $VAR1 = 'final_p';
: $VAR2 = 'p';
: $VAR3 = 'sp_pair';
: $VAR4 = [
:   's',
:   'p'
: ];
: 
: where the keys can be anything, and the values can be arrays
: of any sensible size, including 1. I'm trying to write a loop
: that returns each element of the values in an array, so
: running it on the above it would yield something like @things
: = ('p','s','p'). So, the trick is to always pretend like the
: values are arrays. Then I remembered in the real world a hash
: is only storing references anyway to anonymous arrays, so
: figured it should be easy to dereference these and return
: their guts, as it were. So I wrote (ignoring the returned
: array for now, just printing):
: 
: foreach my $bit (values %BITS) {
: foreach my $thing (@{$bits}) { print $thing\n; } }
: 

my %bits = (
foo = 'final_p',
bar = 'p',
baz = 'sp_pair',
qaz = [ 's', 'p' ],
);

foreach my $bit ( values %bits ) {
if ( ref $bit eq 'ARRAY' ) {
print $_\n foreach @$bit;

} else {
print $bit\n;
}
}


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328



___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs