Parse text file and store fields in hash table in

2019-09-03 04:27发布

i have a problem with my perl script which read a text file and store the first field separated by a tabulation.

Here an example: texte input file :

@ ries bibliothèques électroniques à travers tout le    1   0.000012706627117
a a pour les ressortissants des nouveaux états  1   0.000006796917515
a abandonné dès l abord avec dilettantisme l    1   0.000009584625169
a abandonné la culture du lin à fibres  1   0.000009718010335
a abandonné le secteur de la pêche aux  1   0.000006983488644

So the script must store the first fields from each line and store them in a hash table here: '@ ries bibliothèques électroniques à travers tout le', 'a a pour les ressortissants des nouveaux états', 'a abandonné dès l abord avec dilettantisme', 'a abandonné la culture du lin à fibres', 'a abandonné le secteur de la pêche aux'.

Here's the script:

#!/usr/bin/perl -w

# use strict;

my %hash = ();

open (my $fh,"<", "tst") or die "Can't open the file: ";

while (my $line =<$fh>){

chomp ($line);

my ($key)=split("\t", $line);

my $val;

$hash{$key} =$val;

}

print "\n";

while ( my ($key, $val) = each(%hash) ) {

print "$key => $val\n";

  }

The problem is that this script does not work :( Any idea please?

1条回答
Emotional °昔
2楼-- · 2019-09-03 04:36

This simple (moderately simple) adaptation of your code:

#!/usr/bin/perl -w
use strict;

my %hash = ();
my $file = "tst";

open (my $fh, "<", $file) or die "Can't open the file $file: ";

while (my $line =<$fh>)
{
    chomp ($line);
    my($key, $count, $number) = split("\t", $line);
    $hash{$key} = [ $count, $number ];
}

while (my($key, $val) = each(%hash))
{
    print "$key => @{$val}\n";
}

given the input file:

@ ries bibliothèques électroniques à travers tout le       1       0.000012706627117
a a pour les ressortissants des nouveaux états    1       0.000006796917515
a abandonné dès l abord avec dilettantisme l      1       0.000009584625169
a abandonné la culture du lin à fibres    1       0.000009718010335
a abandonné le secteur de la pêche aux    1       0.000006983488644

(where multiple blanks represent a tab in the file, gives the output:

a a pour les ressortissants des nouveaux états => 1 0.000006796917515
@ ries bibliothèques électroniques à travers tout le => 1 0.000012706627117
a abandonné la culture du lin à fibres => 1 0.000009718010335
a abandonné dès l abord avec dilettantisme l => 1 0.000009584625169
a abandonné le secteur de la pêche aux => 1 0.000006983488644

If that's not what you're after, maybe you need to specify what value should be stored with each key.

The my($key, $count, $number) part saves three fields per line into the named variables. The line:

$hash{$key} = [ $count, $number ];

creates a hash entry with the string $key storing an array reference, where the array contains two values, $count and $number. And the line

print "$key => @{$val}\n";

prints the array referenced by $val.


The requirements are still unclear, but apparently, only the key needs to be stored. It is not clear whether it is sufficient to store it in an array, or whether it must be stored in a hash.

Array

#!/usr/bin/perl -w
use strict;

my @array = ();
my $file = "tst";

open (my $fh, "<", $file) or die "Can't open the file $file: ";

while (my $line =<$fh>)
{
    chomp ($line);
    my($key) = split("\t", $line);
    push @array, $key;
}

foreach my $key (@array)
{
    print "$key\n";
}

Hash

#!/usr/bin/perl -w
use strict;

my %hash = ();
my $file = "tst";

open (my $fh, "<", $file) or die "Can't open the file $file: ";

while (my $line =<$fh>)
{
    chomp ($line);
    my($key) = split("\t", $line);
    $hash{$key} = 1;
}

foreach my $key (sort keys %hash)
{
    print "$key\n";
}
查看更多
登录 后发表回答