Hello all! I'm just getting started at learning Perl and am converting a Java program of mine to Perl for both learning and because I think Perl should handle the task well. I've read through a couple books on Perl but am still getting my Perl-coding legs (and will be for some time, it seems).
Here we are: I'm creating a Tree::Trie that reads from a 4-column CSV to create a lexicon. The first column is the actual word to add to the tree, and works fine. But now I am trying to add the other columns (starting with just one) as data onto that node and could use some help. Here's what I've got:
#!/usr/bin/perl use feature(say); use strict; use warnings; use Text::CSV; use Tree::Trie; my $file = $ARGV[0] or die "Need to get CSV file on the command line\n +"; my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, }); my($trie) = new Tree::Trie; open(my $data, '<:encoding(utf8)', $file) or die "Could not open '$fil +e' $!\n"; while (my $fields = $csv->getline( $data )) { $trie->add_data(($fields->[0])=>($fields->[1])); my(@sent) = $trie->lookup_data($fields->[0]); #<-- only seems to g +et the right data in array context printf "Just added %s\n with sentiment %s\n", $fields->[0], @sent[ +1]; } if (not $csv->eof) { $csv->error_diag(); }
I just arrived at this and it actually works, but it came after hours of failing to get a data retrieval in scalar context to function; it would always just return the word again.
So, I have two questions: first, what do I need to do to get a single piece of data out in scalar context with Tree::Trie? I have a suspicion that I am making some Perl-novice mistake in that.
Second, attaching and retrieving multiple pieces of data for a word. I know it will have to do with attaching an array at the data I'm adding; can anyone give me a friendly example of how the attachment/retrieval would look in that case?
Thank you much! I am pleased to be here with the monks.
In reply to Scalar data & collection data with Tree::Trie by Endless
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |