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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.