Domanda

Ho uno script Perl che sta contando il numero di occorrenze di varie stringhe in un file di testo. Voglio essere in grado di verificare se una determinata stringa non è ancora una chiave nell'hash. C'è un modo migliore per farlo del tutto?

Ecco cosa sto facendo:

foreach $line (@lines){
    if(($line =~ m|my regex|) )
    {
        $string = $1;
        if ($string is not a key in %strings) # "strings" is an associative array
        {
            $strings{$string} = 1;
        }
        else
        {
            $n = ($strings{$string});
            $strings{$string} = $n +1;
        }
    }
}
È stato utile?

Soluzione

Credo di controllare se esiste una chiave in un hash che fai

if (exists $strings{$string}) {
    ...
} else {
    ...
}

Altri suggerimenti

Consiglierei di non usare if ($ hash {$ key}) poiché non farà ciò che ti aspetti se la chiave esiste ma il suo valore è zero o vuoto.

Bene, l'intero codice può essere limitato a:

foreach $line (@lines){
        $strings{$1}++ if $line =~ m|my regex|;
}

Se il valore non è presente, l'operatore ++ supporrà che sia 0 (e quindi incrementa a 1). Se è già lì, verrà semplicemente incrementato.

Suppongo che questo codice dovrebbe rispondere alla tua domanda:

use strict;
use warnings;

my @keys = qw/one two three two/;
my %hash;
for my $key (@keys)
{
    $hash{$key}++;
}

for my $key (keys %hash)
{
   print "$key: ", $hash{$key}, "\n";
}

Output:

three: 1
one: 1
two: 2

L'iterazione può essere semplificata in:

$hash{

Suppongo che questo codice dovrebbe rispondere alla tua domanda:

use strict;
use warnings;

my @keys = qw/one two three two/;
my %hash;
for my $key (@keys)
{
    $hash{$key}++;
}

for my $key (keys %hash)
{
   print "$key: ", $hash{$key}, "\n";
}

Output:

three: 1
one: 1
two: 2

L'iterazione può essere semplificata in:

$hash{

Suppongo che questo codice dovrebbe rispondere alla tua domanda:

use strict;
use warnings;

my @keys = qw/one two three two/;
my %hash;
for my $key (@keys)
{
    $hash{$key}++;
}

for my $key (keys %hash)
{
   print "$key: ", $hash{$key}, "\n";
}

Output:

three: 1
one: 1
two: 2

L'iterazione può essere semplificata in:

$hash{

Suppongo che questo codice dovrebbe rispondere alla tua domanda:

use strict;
use warnings;

my @keys = qw/one two three two/;
my %hash;
for my $key (@keys)
{
    $hash{$key}++;
}

for my $key (keys %hash)
{
   print "$key: ", $hash{$key}, "\n";
}

Output:

three: 1
one: 1
two: 2

L'iterazione può essere semplificata in:

<*>

(Vedi $ _ in perlvar .) E puoi anche scrivere qualcosa del genere:

<*>

Che riporta ogni tasto la prima volta che viene trovato.

}++ for (@keys);

(Vedi $ _ in perlvar .) E puoi anche scrivere qualcosa del genere:

<*>

Che riporta ogni tasto la prima volta che viene trovato.

}++ or print "Found new value:

Suppongo che questo codice dovrebbe rispondere alla tua domanda:

use strict;
use warnings;

my @keys = qw/one two three two/;
my %hash;
for my $key (@keys)
{
    $hash{$key}++;
}

for my $key (keys %hash)
{
   print "$key: ", $hash{$key}, "\n";
}

Output:

three: 1
one: 1
two: 2

L'iterazione può essere semplificata in:

$hash{

Suppongo che questo codice dovrebbe rispondere alla tua domanda:

use strict;
use warnings;

my @keys = qw/one two three two/;
my %hash;
for my $key (@keys)
{
    $hash{$key}++;
}

for my $key (keys %hash)
{
   print "$key: ", $hash{$key}, "\n";
}

Output:

three: 1
one: 1
two: 2

L'iterazione può essere semplificata in:

<*>

(Vedi $ _ in perlvar .) E puoi anche scrivere qualcosa del genere:

<*>

Che riporta ogni tasto la prima volta che viene trovato.

}++ for (@keys);

(Vedi $ _ in perlvar .) E puoi anche scrivere qualcosa del genere:

<*>

Che riporta ogni tasto la prima volta che viene trovato.

.\n" for (@keys);

(Vedi $ _ in perlvar .) E puoi anche scrivere qualcosa del genere:

<*>

Che riporta ogni tasto la prima volta che viene trovato.

}++ for (@keys);

(Vedi $ _ in perlvar .) E puoi anche scrivere qualcosa del genere:

<*>

Che riporta ogni tasto la prima volta che viene trovato.

Puoi semplicemente andare con:

if(!$strings{$string}) ....
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top