[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Thingies



On Thu, 22 Apr 1999 jacobs@azstarnet.com wrote:

> I've hit a point in my coding where I need to use some references. I've
> got a series of arrays, and an array with references to each array.
> Depending on user input, some (but not necessarily all) of those arrays
> will get initialized, processed, and output. (See code below.)
> 
[ SNIPPAGE ]

> #simulated processing
> for ( my $i = 0 ; $i < @testArrayNames ; $i++ ) {
>   if ( defined ( @{$testArray[$i]} ) ) {
>     print "$testArrayNames[$i]\n";
>     for ( my $j = 0 ; $j < @{$testArray[$i]}  ; $j++) {
>       print "\t$testArray[$i]->[$j]\n";
>     }
>   }
> }
> </code snip>
> 
> Here's my problem: I like to give feedback ( print "Now doing X..."; ),
> so I'd like to be able to get the name of the array being looked up
> without having to have another array with the names (@testArrayNames,
> above). Doing this the way I've done it above feels very kludgey, and
> it seems like there should be a better way -- some way to deref
> $testArray[$i] to get the name of the referenced array.
> 

You've got a bunch of array references, and all of the arrays have names
_associated_ with them. So instead of a list of lists try a hash of lists.

my $hash = {
	'set0' => [ 1, 2, 3, ],
	'set1' => [ 4, 5, 6, ],
};

foreach $name (keys %$hash) {
	print "Processing array ", $name, "\n";
	my $list = $hash->{$name};

	# do stuff with $list
}

__END__

I suggest this because there seems to be no inherent need to have arrays
in a particular order, in your example.

As a further comment, don't call defined() on arrays or hashes. In this
example, you can call exists() to see if a specific key is valid, or just
test the individual array refs.

Arved



===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org