Why not something like this?
my %hash;
while(<SM_FILE>){
chomp $_; #remove the newline
my($key,$value) = split(/,/,$_); #split by commas or whatever
$hash{$key} = $value; #assign the value here
}
foreach(sort keys %hash){
print "$key => $value\n";
}
-----Original Message-----
From: Jamie Risk [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, January 29, 2003 2:32 PM
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?
I've got this so far:
vvvv
#!/usr/bin/perl -w
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);
}
foreach my $key (@desc)
{
print $key, "\n";
}
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};
}
--
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]