On Thu, 8 Nov 2001, Christopher Solomon wrote:
> On Thu, 8 Nov 2001, Tyler Cruickshank wrote:
>
> > I would like to create a hash based on data contained in a file. Can I not use a
>variable as both the key and the value as shown below? When I print as shown I do
>not get a value however if I write the key as: alta_guard => $names[1] then I
>get the correct value printed out.
> >
> > open(NAMES, "d://perl/avalanche/names.txt") || die "Cant open names.txt\n";
> >
> > while(<NAMES>){
> > chomp;
> > @names = split(/-/);
> >
> > %stations = ( $names[0] => $names[1] );
> > @names = ();
> >
> > } # End while.
> >
> > $table = 'alta_guard';
> > print "Text: $stations{$table}\n";
> >
>
> I'm not sure exactly what you want, but I think you want this:
>
> my %stations;
>
> while (<NAMES>) {
> chomp;
> my @names = split(/-/);
> $stations{$names[0]} = $names[1];
> }
>
> Chris
If this is what you want, a more fun way to write it would be:
%stations = map { chomp && split(/-/) } (<NAMES>);
That should do it all in one line.
Chris
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]