> -----Original Message-----
> From: Jamie Risk [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, January 30, 2003 9:32 AM
> To: [EMAIL PROTECTED]
> Subject: creating hash from name/value found in file.
>
>
> Okay, it's 5:25pm and I started programming PERL shortly
> after downloading
> the 5.8.0 tarballs at 2:30pm.
> I'd like to create a hash from a text file that has
> name/value pairs, one
> per line. So far, I'm only capable of reading it in as a list
> - this PERL
> stuff really seems easy - but can I (excuse the c lingo)
> 'cast' or refer to
> my list (@desc) as a hash somehow?
>
Firstly it's good practice to 'use strict;' in all scripts.
> I've got this so far:
>
> vvvv
> #!/usr/bin/perl -w
>
use strict;
my %pairs; # hash to store key/value pairs
my ($key, $value);
> if (0 > $#ARGV)
> {
> print "Need a file (or files) describing where to find states and
> stimuli.\n";
> exit;
> }
>
> foreach my $state_machine (@ARGV)
> {
> chomp $state_machine;
> print "Processing the file \"$state_machine\"\n";
> open SM_FILE, $state_machine || die "can't open
> \"$state_machine\": $!\n";
>
> while (<SM_FILE>)
> {
> chomp;
> unshift (@desc, split);
# not sure what your delimeter is but say it is an equal sign
($key,$value) = split(/=/, $_);
$pairs{$key} = $value;
> }
>
[snipped now redundant section]
>
> close SM_FILE || die "this is wierd, can't close \"$state_machine\":
> $!\n";
> }
> ^^^^
>
> The "foreach my $key(@desc)" I'd prefer to be something like:
> foreach my $key (sort(keys(%desc)))
> {
> print $key,'=',$desc{$key};
> }
>
while (($key,$value) = each %pairs)
{
print "$key=$value\n";
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]