Reputation: 698
How do I print $stopwords? It seems to be a string ($) but when I print it I get: "HASH(0x8B694)" with the memory address changing on each run.
I am using Lingua::StopWords and I simply want to print the stop words that it's using so I know for sure what stop words are there. I would like to print these two a file.
Do I need to deference the $stopwords some how?
Here is the code:
use Lingua::StopWords qw( getStopWords );
open(TEST, ">results_stopwords.txt") or die("Unable to open requested file.");
my $stopwords = getStopWords('en');
print $stopwords;
I've tried:
my @temp = $stopwords;
print "@temp";
But that doesn't work. Help!
Last note: I know there is a list of stop words for Lingua::StopWords, but I am using the (en) and I just want to make absolute sure what stop words I am using, so that is why I want to print it and ideally I want to print it to a file which the file part I should already know how to do.
Upvotes: 0
Views: 247
Reputation: 5279
Have a look at Data::Printer as a nice alternative to Data::Dumper. It will give you pretty-printed output as well as information on methods which the object provides (if you're printing an object). So, whenever you don't know what you've got:
use Data::Printer;
p( $some_thing );
You'll be surprised at how handy it is.
Upvotes: 2
Reputation: 19315
to dereference a hashref :
%hash = %{$hashref}; # makes a copy
so to iterate over keys values
while(($key,$value)=each%{$hashref}){
print "$key => $value\n";
}
or (less efficient but didactic purpose)
for $key (keys %{$hashref}){
print "$key => $hashref->{$key}\n";
}
Upvotes: 3
Reputation: 183301
getStopWords
returns a hashref — a reference to a hash — so you would dereference it by prepending %
. And you actually only want its keys, not its values (which are all 1
), so you would use the keys
function. For example:
print "$_\n" foreach keys %$stopwords;
or
print join(' ', keys %$stopwords), "\n";
You can also skip the temporary variable $stopwords
, but then you need to wrap the getStopWords
call in curly-brackets {...}
so Perl can tell what's going on:
print join(' ', keys %{getStopWords('en')}), "\n";
Upvotes: 1
Reputation: 3582
$
doesn't mean string. It means a scalar, which could be a string, number or reference.
$stopwords
is a hash reference. To use it as a hash, you would use %$stopwords
.
Use Data::Dumper
as a quick way to print the contents of a hash (pass by reference):
use Data::Dumper;
...
print Dumper($stopwords);
Upvotes: 7