Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Scalar data & collection data with Tree::Trie

by tangent (Parson)
on Jul 04, 2013 at 16:23 UTC ( [id://1042486]=note: print w/replies, xml ) Need Help??


in reply to Scalar data & collection data with Tree::Trie

failing to get a data retrieval in scalar context to function; it would always just return the word again.
I tried your code and using scalar context worked for me, i.e. it returned the data associated with the word, not the word itself.
use Tree::Trie; my $trie = new Tree::Trie; $trie->add_data( word => 'data'); my @sent = $trie->lookup_data('word'); print "List context: @sent\n"; my $data = $trie->lookup_data('word'); print "Scalar context: $data\n";
Output:
List context: word data Scalar context: data
Maybe you could post up some of your input file? It would help to answer your second question as well.

Update: Is it possible you are using "scalar context" like this:

my ($data) = $trie->lookup_data($fields->[0]);
If so, that will indeed return the word and not the data, you need to leave out the brackets around $data

Replies are listed 'Best First'.
Re^2: Scalar data & collection data with Tree::Trie
by Endless (Beadle) on Jul 04, 2013 at 21:06 UTC

    Thanks! You are exactly right about why my scalar wasn't working; I was calling it as 'my ($var).' Can you give me a quick line on why I should/shouldn't have the parenthesis on that?

    I seem to have the multi-data working now, and I'll show you what I did. I suspect it is pretty sloppy, un-idiomatic code, so any suggestions for a cleaner implementation are welcome. Here we are. First, my CSV format (for reference):

    "PrimaryTest","normalSentiment","negSentiment","topics" "a waste",-1,0,""

    Now my code:

    while (my $fields = $csv->getline( $data )) { my $word = $fields -> [0]; my $pos_sent = $fields->[1]; my $neg_sent = $fields->[2]; my $topics = $fields->[3]; my @word_info = ($pos_sent, $neg_sent, $topics); # Trying with a reference my $pnt_word_info = \@word_info; $trie->add_data($word => $pnt_word_info); my $info = $trie->lookup_data($word); printf "Just added %s\n Sentiment: %s \t Neg Sentiment: %s \t Topi +c: %s\n", $word, $info->[0], $info->[1], $info->[2]; }
      my ($var) is imposing list context, $var will contain the first element of the list returned (in this case the word).

      With regards to your code there's nothing really wrong with it but, unless you need the intermediate variables for something else, it could be shortened to:

      while (my $fields = $csv->getline( $data )) { my $word = $fields->[0]; $trie->add_data( $word => [ $fields->[1], $fields->[2], $fields->[ +3] ] ); my $info = $trie->lookup_data($word); printf "Just added %s\n Sentiment: %s \t Neg Sentiment: %s \t Topi +c: %s\n", $word, $info->[0], $info->[1], $info->[2]; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1042486]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-20 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found