1

I am trying my own perl "AI hello world" and having a hard time understanding the results of AI::Classifier::Text::FileLearner;

I believe I have gone astray in iterating through the hash references.

#!/usr/bin/perl

use utf8;
use strict;
use warnings;

use AI::Classifier::Text::FileLearner;
my $iterator = AI::Classifier::Text::FileLearner->new( training_dir => '/home/pi/20_newsgroups/talk.politics.guns' );

print "getting ready to classify\n";
my $classifier = $iterator->classifier;

my $key;
my $value;
my %hash = %$classifier;
# traversing the hash using "each" function 
while(($key, $value) = each (%hash)) 
{ 

    # do stuff 
    $value = $hash{$key};
    #print ref{$value};
    if (ref{$value} eq 'HASH') {

    print "$value is ANOTHER hash reference\n";
    my %hash2 = %$value; #derefrence it
    while(($key, $value) = each (%hash2)) 
    {

        # do stuff 
        $value = $hash2{$key};
        print "Value of $key is $value\n";  

    }


    } else {

    print "Value of $key is $value\n";  

    }


} # end of 1st while 

Is there a better more effective way to try to iterate through these hash references so that I can see the results of the training?

gatorreina
  • 864
  • 5
  • 14
  • 2
    "_Is there a better more effective way..._" -- yes: [Data::Dump](https://metacpan.org/pod/Data::Dump) (with its `dd` and `pp`), [Data::Dumper](https://perldoc.perl.org/Data/Dumper.html) (core), and more – zdim Dec 24 '19 at 20:21

0 Answers0