Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by Endless (Beadle)
on Jul 04, 2013 at 21:06 UTC ( #1042525=note: print w/replies, xml ) Need Help??


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

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]; }

Replies are listed 'Best First'.
Re^3: Scalar data & collection data with Tree::Trie
by tangent (Vicar) on Jul 04, 2013 at 23:07 UTC
    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://1042525]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2023-03-29 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (71 votes). Check out past polls.

    Notices?