On 18 Jul 2001 12:06:50 -0400, David Gilden wrote:
>
> Take this Data structure (which is an anonymous hash)
>
> #
> # $bears = {
> #
> #
> # rec0 => {
> # name => 'sweaterie',
> # type => 'sweater',
> # color => 'golden brown',
> # food => 'mixed berries',
> # }, # ect
> # };
>
>
> I am trying to build from a flat file dynamically:
>
> # the text file looks like:
> # name1|type|color|food
> # name2|type|color|food
>
> open (DATA, "bear_data.txt") || die "Could not access file $!";
>
> local $/; # undefine the builtin var $/
> $tmp = <DATA>;
> @tmp = split (/\n/,$tmp);
>
>
> my $n = 0;
> my $bears = {};
>
> foreach $line (@tmp){
> my ($name,$type,$color,$food); # is this correct?
> ($name,$type,$color,$food) = split (/|/, $line);
>
> $bears->{"rec$n"} = {};
> # problem here #
> $bears->{"rec$n"}{
> name => $name;
> type => $type;
> color => $color;
> food => $food;
>
> };
> $n++;
> }
>
<snip />
What I think you want to do here is
$bears->{"rec$n"} = {
name => $name,
type => $type,
color => $color,
food => $food
};
You can then access the third bear's food like this:
print $bears->{"rec2"}{food}, "\n";
QUESTIONS: Why are you using a hash instead of an array for the first
level? Why are you using a reference instead of an actual hash or array
variable?
--
Today is Prickle-Prickle, the 53rd day of Confusion in the YOLD 3167
P'tang!
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]