Beri-Veera-ext, Reddy (n.a.) wrote:
> Hello,
>    I want to add to Associative Array dynamically (means=> I don't exact 
> size of Associative Array).
>  
> Ex: In test.txt I have the fallowing data.
>         red,danger
>         blue,going smooth
>         yellow, pending
>         Green, accept.
>          ....etc
>  
> I have to add these values to Associative Array
> like
>   %info= (red =>danger
>         blue =>going smooth
>         yellow => pending
>         Green=>accept.
>        ...etc)
>  
> For these I have read the test.txt line by line and add to %info. Please 
> guide me how to do this....

use strict;
use warnings;

my @lines = (   # phoney test data simulating file data
   "red,danger\n",
   "blue,going smooth\n",
   "yellow, pending\n",
   "Green, accept\n",
);

my %info = ();
foreach (@lines) {      # would normally be: while (<IN>) { # reading from file
        chomp;
        my @a = split /\s*,\s*/;
        $info{$a[0]} = $a[1];
}
print "$_ => $info{$_}\n" foreach keys %info;

__END__
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to