use Data::Dumper;

my $bears = {}; # declare here so it'll be available after while()
my $n = 0;

# for examples sake we're using DATA internal filehandle
while(<DATA>)
{
        chomp;
        my ($name, $type, $color, $food) =  split /\|/;

        $bears->{"rec$n"} = {};

        %{ $bears->{"rec$n"} } = (
                                  'name'  =>  $name,
                                  'type'  =>  $type,
                                  'color' =>  $color,
                                  'food'  =>  $food,
                                 );

} continue { $n++ }

print Dumper($bears);

__DATA__
foo|black bear|black|berries
bar|grizzly|greyish|humans


-----------------------------------------

Have fun!
Luke

PS - look everyone - a continue block! :-)

On Wed, 18 Jul 2001, 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++;
> }
>
> Please point out what I missed or if there is a better way of accomplishing this,
> Thanks,
> Dave
>
> --------------------------------------------
> Looking for Web Talent, You found it!
> portfolio: www.coraconnection.com/web/
> email: [EMAIL PROTECTED]
> tel/fax: (860) 231-9988
> --------------------------------------------
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to