Re: vars as key/value in a hash
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(){ > chomp; > @names = split(/-/); > > %stations = ( $names[0] => $names[1] ); > @names = (); > > } # End while. > > $table = 'alta_guard'; > print "Text: $stations{$table}\n"; I would do it like this: %stations = (); open(NAMES, "d://perl/avalanche/names.txt") || die "Cant open names.txt\n"; while(){ chomp; @names = split(/-/); $stations{$names[0]} = $names[1]; } # End while. $table = 'alta_guard'; print "Text: $stations{$table}\n"; -- Brett http://www.chapelperilous.net/ To give happiness is to deserve happiness. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: vars as key/value in a hash
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(){ > 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 () { chomp; my @names = split(/-/); $stations{$names[0]} = $names[1]; } Chris > Thanks! > > -tyler > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: vars as key/value in a hash
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(){ > > 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 () { > 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(/-/) } (); That should do it all in one line. Chris -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: vars as key/value in a hash
FYI, Tyler, the reason that this will do what you want is because you're original program wasn't doing what you thought. %stations = ( $names[0] => $names[1] ); This creates a list of two elements and assigns it to the hash named %stations. Therefore, $names[0] becomes the one and only key in %stations and $names[1] becomes the one and only value. If %stations did not exist before, it does exist now; if it _did_ exist, then its former values are wiped out. That's the key point...every time you went through the loop, you were wiping out all the previous data you had read in, so the table only ever contained one key and one value. Therefore, since (I assume) 'alta_guard' was not the last entry in the file you were reading from, it did not appear in the table when you were finished. Notice that Brett's version saves the values from one iteration of the loop to the other, because you aren't reinitializing the hash. HTH, Dave On Thu, 8 Nov 2001, Brett W. McCoy wrote: > On Thu, 8 Nov 2001, Tyler Cruickshank wrote: > > > open(NAMES, "d://perl/avalanche/names.txt") || die "Cant open names.txt\n"; > > > > while(){ > > chomp; > > @names = split(/-/); > > > > %stations = ( $names[0] => $names[1] ); > > @names = (); > > > > } # End while. > > > > $table = 'alta_guard'; > > print "Text: $stations{$table}\n"; > > I would do it like this: > > %stations = (); > open(NAMES, "d://perl/avalanche/names.txt") || die "Cant open names.txt\n"; > > while(){ > chomp; > @names = split(/-/); > $stations{$names[0]} = $names[1]; >} # End while. > > $table = 'alta_guard'; > print "Text: $stations{$table}\n"; > > -- Brett > http://www.chapelperilous.net/ > > To give happiness is to deserve happiness. > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]