EchoAngel has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I'm parsing this huge file with a lot of hierarchy in it. For example, I would save stuff like $FoodHash{colour}{size}{taste} = line location Sometimes, I get a data for
$FoodHash{green}{big} = "watermelon"; $FoodHash{green}{big}{sweet} = "1234";
sometimes when i go back to the $FoodHash{green}{big}, i would lose the value. Does anyone know why?

Replies are listed 'Best First'.
Re: Hash problems
by ysth (Canon) on Aug 27, 2004 at 19:00 UTC
    Those two lines can't both be represented. $FoodHash{green}{big} can't store both a string "watermelon", and a hashref { sweet => "1234" }. Consider adding {value} to the end of all your data, so that everything is a hashref except when the key is "value". (Use some other string if "value" will conflict with your data.) So your assignments become:
    $FoodHash{green}{big}{value} = "watermelon"; $FoodHash{green}{big}{sweet}{value} = "1234";
      Instead of value, you could always use the empty string:
      $FoodHash{green}{big}{""} = "watermelon"; $FoodHash{green}{big}{sweet}{""} = "1234";
      -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/