Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Nested Cycle?

by remluvr (Sexton)
on Mar 02, 2012 at 16:14 UTC ( #957507=perlquestion: print w/ replies, xml ) Need Help??
remluvr has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone.
I have another question, it's like I can't actually grasp how to do this task I have. I have a complex structure, like the following:

$VAR1 = { 'name1' => [ 'property1,8.8016', 'property2,4.5016', 'property3,4.0873', 'property4,7.0726', 'property5,9.5121' ], 'name2' => [ 'property6,5.6217', 'property2,4.7993', 'property7,4.2885', 'property3,11.1250', ], 'name3' => [ 'property8,5.6217', 'property2,4.7993', 'property1,4.2885', 'property9,11.1250', ] }

I should choose two words (say, in this case, name1 and name2). I should sum each score of the properties of name1 that are also properties of name2. In the example above, my sum should be the following:
4.5016+4.0873 because this are the values of property2 and property3 in name1 and property2 and property3 are the only 2 properties of name1 that occur in name2.
My problem is, I don't know where to start.
Would you please give me some input to help me, so I can get off this empasse?
Thanks,
Giulia

Comment on Nested Cycle?
Download Code
Re: Nested Cycle?
by JavaFan (Canon) on Mar 02, 2012 at 16:23 UTC
    My problem is, I don't know where to start.
    You could start by changing the structure. You now have a hash, whose values are array(ref)s containing strings you have to split. Turn it into a 2 level hash:
    { name1 => {property1 => 8.8016, property2 => 4.5016, property3 => 4.0873, property4 => 7.0726, property5 => 9.5121,}, name2 => { ... }, name3 => { ... } };
    You can then easily find your answer (untested):
    my $word1 = "..."; my $word2 = "..."; my $data = { ... }; # Contains the data above my $sum = 0; foreach my $property (keys %{$$data {$word1}}) { $sum += $$data{$word1}{$property} if exists $$data{$word2}{$proper +ty}; }
Re: Nested Cycle?
by jethro (Monsignor) on Mar 02, 2012 at 16:26 UTC

    Is this homework for school?

    The most complex part of your problem is: You have two arrays and you want to extract only those properties of the first array that are also in the second array. Write a subroutine that accepts two array references as parameters. In that subroutine loop through the first array and for each property search the second loop (hint: grep() is good for this).

    When you have that subroutine written, the rest is easy: Just write a main routine that gets the two array references and calls the subroutine

Re: Nested Cycle?
by tangent (Curate) on Mar 02, 2012 at 16:44 UTC
    To work with your existing data structure:
    my $name1 = 'name1'; # or whatever my $name2 = 'name2'; # or whatever my $sum = 0; # turn Hash of Arrays into Hash of Hashes my %HOH; while (my($name,$aref) = each %{$VAR1} ) { for my $item (@$aref) { my($prop,$score) = split(',',$item); $HOH{$name}{$prop} = $score || 0; } } my $first = $HOH{$name1}; my $second = $HOH{$name2}; while (my($prop,$score) = each %{$first} ) { $sum += $score if exists $second->{$prop}; } print "Sum of $name1 in $name2 is: $sum\n"; # Prints: Sum of name1 in name2 is: 8.5889
Re: Nested Cycle?
by TJPride (Pilgrim) on Mar 02, 2012 at 17:53 UTC
    use strict; use warnings; my $w1 = 'name1'; my $w2 = 'name2'; my $hash = { 'name1' => [ 'property1,8.8016', 'property2,4.5016', 'property3,4.0873', 'property4,7.0726', 'property5,9.5121' ], 'name2' => [ 'property6,5.6217', 'property2,4.7993', 'property7,4.2885', 'property3,11.1250' ], 'name3' => [ 'property8,5.6217', 'property2,4.7993', 'property1,4.2885', 'property9,11.1250' ] }; my %sums; for (@{$hash->{$w2}}) { @_ = split /,/; $sums{$_[0]} = $_[1]; } for (@{$hash->{$w1}}) { @_ = split /,/; print "$_[0] = $_[1] + $sums{$_[0]} = " . ($_[1] + $sums{$_[0]}) . + "\n" if $sums{$_[0]}; }
Re: Nested Cycle?
by remluvr (Sexton) on Mar 03, 2012 at 09:39 UTC

    Thank you all!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://957507]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (14)
As of 2015-01-29 16:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My top resolution in 2015 is:

















    Results (243 votes), past polls