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

This node falls below the community's minimum standard of quality and will not be displayed.
  • Comment on Re: Can I determine if the value of a variable is the same as an arrays name?
  • Download Code

Replies are listed 'Best First'.
Re: Answer: Can I determine if the value of a variable is the same as an arrays name?
by Jenda (Abbot) on Jun 18, 2008 at 21:42 UTC

    I find this very inventive. So you know about hashes, you use a hash with the $category being the key yet you insist on using symbolic references? If you already have the hash, why don't you store the array there?

    use strict; use Data::Dumper; open (FOOD, "<", "food.txt") or die "Can't open food.txt: $^E"; my %categories; while (<FOOD>) { chomp; my ($category, $item) = split(/\s+/); push(@{$categories{$category}}, $item); } print ">$_<\n",Dumper($categories{$_}) for keys %categories; close FOOD;
    No variables springing into existence and overwriting other stuff willy-nilly, smaller memory footprint and quicker execution.

    Symbolic references are (almost) never the right solution. And you do not need to worry why I said almost for a few more years. Believe me!

Re: Answer: Can I determine if the value of a variable is the same as an arrays name?
by CountZero (Bishop) on Jun 19, 2008 at 05:16 UTC
    Why would you do BEGIN { use Data::Dumper }; if use is already BEGIN { require Module; import Module LIST; }?

    Also, the 3 argument form of open is generally considered the better practice.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      And error checking (or die "Can't open file .. $!")