Highlight Perl hashrefs

Use with Text::Ngramme

my $c = ngram_counts($text, 3);
my %ct = %($c);

which does not work ( Scalar found where operator expected). I think this is a combination of not knowing what I am doing (still not very good with Perl) and embarrassing about what exactly I get from Text :: Ngram output. Help? I just want to look at the generated n-grams:

my @keys = sort {$ct{$a} cmp $ct{$b} } keys %ct;
foreach my $k (@keys) {
    print "$k: $ct{$k}\n"
}

Edit: Stupid mistake on my part, thanks everyone.

+5
source share
4 answers

Use curly braces to dereference a hash link:

my %ct = %{ $ct };  # %$ct would also work

And you probably want to use <=>ASCII-betic for numerical sorting instead of sorting cmp.

+12
source

Use curly braces:

my %ct = %{ $c };
+6
source

, .

my $c = ngram_counts($text, 3);

my @keys = sort {$c->{$a} <=> $c->{$b} } keys %$c;
foreach my $k (@keys) {
    print "$k: $c->{$k}\n"
}

. http://perlmonks.org/?node=References+quick+reference .

+3

?

my $c = ngram_counts({}, $text, 3);
0

All Articles