Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Counting/Matching Terms In Arrays contained within a Hash

by sdslrn123 (Novice)
on Jun 21, 2006 at 13:48 UTC ( #556665=perlquestion: print w/replies, xml ) Need Help??
sdslrn123 has asked for the wisdom of the Perl Monks concerning the following question:

Sorry another update: Is there a way to calculate the total number of terms altogether. I want this information to convey the information as a percentage.
print "$member can be found " . scalar(@{$family{$member}}) / ? . ": "
#!/usr/bin/perl use Data::Dumper; # only for teaching purpose $Data::Dumper::Indent = 1; # dito :-) while(<DATA>) { chop; my($familyname,$memberstring) = split /\s--\s/,$_; my @members = split /, /,$memberstring; # tell each member that it's got a new family foreach my $member(@members) { push(@{$family{$member}},$familyname); } } print Dumper(\%family); # show the data structure in %family # now output a list of members with their families foreach my $member(sort keys %family) { print "$member can be found " . scalar(@{$family{$member}}) . ": " . join(" ", @{$family{$member}}) . "\n"; }
Thank You. Genius. Some heavy PERLDOC reading tonight!! Updated. Sorry about confusion with variable names. Basically my data is subject to change but will follow a format as follows where I will have a unique "Family Name" followed by "--" followed by various "family members". The contents of the data are unknown to me apart from the format. Data is a text file. Could consist of as follows:
FLINTSTONES=BARNEY, FRED, WILMA JETSONS=MAX, TONY, WILMA SIMPSONS=LISA, BARNEY, WILMA, HOMER ALCATRAZ=ELIJAH, MAX, WILMA
I know I can grab this into an array called @Dopli. And split each line $unix at "--" into an array @trainer Is there a way of pushing each array into a hash using $trainer[0] and $trainer1.
sub nice_list { return '' if @_ == 0; return shift if @_ == 1; my $last = pop; return join(', ', @_) . " and $last"; } my $line; my @data; my @family; my @members; foreach $line (@data){ @family = split ('=', $line); @members = split (',', $family[1]); my %is_eaten_by = ( $family[0] => [ (@members) ], ); foreach my $fruit (keys %is_eaten_by) { my $eaters = $is_eaten_by{$fruit}; my $num_eaters = @$eaters; print("$num_eaters ${fruit}: ", (@$eaters), "\n"); } }
Ultimately I am looking to print:
BARNEY can be found 2: FLINTSTONES SIMPSONS FRED can be found 1: FLINTSTONES WILMA can be found 4: FLINTSTONES JETSONS SIMPSONS ALCATRAZ MAX can be found 2: JETSONS ALCATRAZ and so forth...
At the moment am just getting what seems the number of terms of each array related to the key. Thanks for any kind of help or advice.

Replies are listed 'Best First'.
Re: Counting/Matching Terms In Arrays contained within a Hash
by shmem (Canon) on Jun 21, 2006 at 14:29 UTC
    Here's something to study.
    #!/usr/bin/perl use Data::Dumper; # only for teaching purpose $Data::Dumper::Indent = 1; # dito :-) while(<DATA>) { chop; my($familyname,$memberstring) = split /\s--\s/,$_; my @members = split /, /,$memberstring; # tell each member that it's got a new family foreach my $member(@members) { push(@{$family{$member}},$familyname); } } print Dumper(\%family); # show the data structure in %family # now output a list of members with their families foreach my $member(sort keys %family) { print "$member can be found " . scalar(@{$family{$member}}) . ": " . join(" ", @{$family{$member}}) . "\n"; } __DATA__ FLINTSTONES -- BARNEY, FRED, WILMA JETSONS -- MAX, TONY, WILMA SIMPSONS -- LISA, BARNEY, WILMA, HOMER ALCATRAZ -- ELIJAH, MAX, WILMA
    Download that code an run it; it will produce the following output:
    $VAR1 = { 'ELIJAH' => [ 'ALCATRAZ' ], 'FRED' => [ 'FLINTSTONES' ], 'HOMER' => [ 'SIMPSONS' ], 'BARNEY' => [ 'FLINTSTONES', 'SIMPSONS' ], 'MAX' => [ 'JETSONS', 'ALCATRAZ' ], 'LISA' => [ 'SIMPSONS' ], 'WILMA' => [ 'FLINTSTONES', 'JETSONS', 'SIMPSONS', 'ALCATRAZ' ], 'TONY' => [ 'JETSONS' ] }; BARNEY can be found 2: FLINTSTONES SIMPSONS ELIJAH can be found 1: ALCATRAZ FRED can be found 1: FLINTSTONES HOMER can be found 1: SIMPSONS LISA can be found 1: SIMPSONS MAX can be found 2: JETSONS ALCATRAZ TONY can be found 1: JETSONS WILMA can be found 4: FLINTSTONES JETSONS SIMPSONS ALCATRAZ
    If you need more advice or explanation, fell free to ask.

    But before that - read the manual pages perldata, perlref and the one for Data::Dumper

    _($_=" "x(1<<5)."?\n".q/)Oo.  G\        /
                                  /\_/(q    /
    ----------------------------  \__(m.====.(_("always off the crowd"))."
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Counting/Matching Terms In Arrays contained within a Hash
by Jaap (Curate) on Jun 21, 2006 at 14:08 UTC
    Is this a typo?
    @trainer = split ('=', $unix);
    One would expect '--'.
    Secondly, if you use clearer variable names, it's much, MUCH easier for us to read and help you.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://556665]
Approved by jdtoronto
help
Chatterbox?
[choroba]: I have no idea :-( I used to post to photobucket.com, but they don't seem to feature "private" pictures in the free version now
[Discipulus]: dazz i'm not an experts but i think it would be possible
[Corion]: dazz: I think Image::Magick can "read" from an in-memory filehandle
[Corion]: choroba: No, some general admin discussion of how to handle (company) user accounts
[Discipulus]: might be it is necessary to pass the pic data like MIME::Base64:: encode( data..
[Discipulus]: if you have a jpeg data: or as wise Corion said IM ca n read directly your handle

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2017-03-27 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (317 votes). Check out past polls.