changma_ha has asked for the wisdom of the Perl Monks concerning the following question:
i know it this question seem to be silly but i am struck here. i have a text file like
1 human 2 flower 3 fruits 5 human 6 car 9 flower asdgjhashdnh gsdh sjs klja
now i want my output to be like 1= human 2= flower 3 =fruits 5 =human 6= car 9= flower
my code is :
#!/usr/bin/perl #use strict; #use warnings; my %hash; open (my $fh,"<", "c:/asso2.txt") or die "Can't open the file: "; while (my $line =<$fh>){ chomp ($line); if ($line =~ /\d\s+\w/){ my ($index, $data)=split (/\s+/,$line); #split on the basis + of space $hash{$index} =$data; #Assign the value to key } } foreach my $i(sort keys %hash){ print "$i =$hash{$index}\n"; }
plz give me wiser . what can be the mistake
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: creating a hash from a text file.
by moritz (Cardinal) on Jul 27, 2010 at 07:06 UTC | |
by AnomalousMonk (Archbishop) on Jul 27, 2010 at 07:45 UTC | |
Re: creating a hash from a text file.
by AnomalousMonk (Archbishop) on Jul 27, 2010 at 07:11 UTC | |
Re: creating a hash from a text file.
by johngg (Canon) on Jul 27, 2010 at 10:52 UTC | |
by AnomalousMonk (Archbishop) on Jul 27, 2010 at 18:20 UTC | |
by johngg (Canon) on Jul 27, 2010 at 22:30 UTC | |
Re: creating a hash from a text file.
by Generoso (Prior) on Jul 27, 2010 at 15:19 UTC |
Back to
Seekers of Perl Wisdom