Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Can I determine if the value of a variable is the same as an arrays name?

by jreades (Friar)
on Nov 29, 2000 at 21:16 UTC ( [id://43952]=note: print w/replies, xml ) Need Help??


in reply to How can I use the value of a scalar as the name of an array variable?

You can do this using symbolic references, but running under -w or use strict this will often generate a number of warnings.

Here's an example:

$two = 'test'; @three = ('test'); my @array = ('one', 'two', 'three'); foreach (@array) { if (@$_) { print "Array exists: $_\n"; } if ($$_) { print "Scalar exists: $_\n"; } }

And this is how it would apply to your problem:

my @fruit = (); my @meat = (); my @dairy = (); open (FOOD, "./food.txt") while (<FOOD>) { chomp; ($category, $item) = split(/\$/, $_); push(@{$category}, $item) if (@{$category}); }

So yes, it's do-able, but it's not always the best programming method although it seems to fit here and wouldn't generate warnings as you've declard the arrays you're trying to assign to and not initializing them on the fly.

Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://43952]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-19 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found