Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: hash of arrays

by spazm (Monk)
on Oct 26, 2011 at 09:09 UTC ( [id://933828]=note: print w/replies, xml ) Need Help??


in reply to hash of arrays

Your code looks roughly correct for the behavior you are expecting.

Some minor changes:

  • The second chomp call should be removed.
  • added Data::Dumper to inspect to dump output
  • attached $key lexical to a smaller scope
  • add error checking to file open
  • close filehandle when done reading it
  • lexical filehandle $IN1 rather than global filehandle IN1
#!/usr/bin/perl use strict; use warnings; use open IN => ':utf8'; use open OUT => ':utf8'; use Encode; use Data::Dumper; my $file1 = shift; my %hash; open( my $IN1, "<:encoding(utf8)", $file1 ) or die; while (<$IN1>) { chomp; my ($video, $p) = split /\t/; push( @{$hash{$video}}, $p ); #print "array of hash of $video <==== $p\n"; } close $IN1; foreach my $key ( keys %hash ) { print "$key: @{ $hash{$key} }\n" } print Dumper \%hash;
output:
mv89psg6zh4: A bird in a sink keeps getting under the running water fr +om a fauc et. A bird is bathing in a sink. A bird is splashing around under a ru +nning fau cet. A bird is bathing in a sink. A bird is standing in a sink drinkin +g water t hat is pouring out of the facet. A faucet is running while a bird stan +ds in the sink below. A bird is playing in a sink with running water. + A bird is playing in tap water. A bird is bathing in the sink. A bir +d is taking a bath. $VAR1 = { 'mv89psg6zh4' => [ 'A bird in a sink keeps getting under the r +unning water from a faucet.', 'A bird is bathing in a sink.', 'A bird is splashing around under a running + faucet.', 'A bird is bathing in a sink.', 'A bird is standing in a sink drinking wate +r that is pouring out of the facet.', 'A faucet is running while a bird stands in + the sink below.', 'A bird is playing in a sink with running w +ater.', 'A bird is playing in tap water.', 'A bird is bathing in the sink.', 'A bird is taking a bath.' ] };

Replies are listed 'Best First'.
Re^2: hash of arrays
by Anonymous Monk on Oct 26, 2011 at 09:14 UTC
    This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi Copyright 1987-2009, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge.
    system:
    Linux tammuz 2.6.32-131.12.1.el6.x86_64 #1 SMP Sun Jul 31 16:44:56 EDT + 2011 x86_64 x86_64 x86_64 GNU/Linux
    And then that's extremely strange!!!!
Re^2: hash of arrays
by Anonymous Monk on Oct 26, 2011 at 09:24 UTC
    I do get the output of dump correct but the same output for the other :
    A bird is taking a bath. sink..h running water.sink below.g out of th +e facet.cet.
      what output do you get if you change your print loop to
      foreach my $key ( keys %hash ) { #print "$key: @{ $hash{$key} }\n" print "$key: \n"; foreach (@{$hash{$key}}) { print $_ , "\n"; } }
      Does each line get printed correctly, or are the lines garbled as in your old output?
        Now the output is correct but when I want to use a loop as this:
        foreach my $key ( keys %hash ) { #print "$key: @{ $hash{$key} }\n" #print "$key: \n"; for my $i (1..$#{$hash{$key}}) { print $hash{$key}[0],"\t" ,$hash{$key}[$i], "\n"; } }
        the output would be again corrupted as:
        A bird iA bird is bathing in a sink. the running water from a faucet. A bird iA bird is splashing around under a running faucet.m a faucet. A bird iA bird is bathing in a sink. the running water from a faucet. A bird iA bird is standing in a sink drinking water that is pouring ou +t of the facet. A bird iA faucet is running while a bird stands in the sink below.et. A bird iA bird is playing in a sink with running water.from a faucet. A bird iA bird is playing in tap water.e running water from a faucet. A bird iA bird is bathing in the sink.he running water from a faucet. A bird iA bird is taking a bath.nder the running water from a faucet.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-03-19 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found