简体   繁体   中英

Perl Hash of Hash Output

I'm reading a file. I want a hash that gives me the first number of a line as a key to a hash of all the numbers of the rest of the line to 1.

I believe I'm adding the hash correctly, because Dumper prints correctly. However, print "$first $secondID\\n" is not giving me any output.

while (<FILE>) {

    chomp $_;

    if (/(\d+)\t(.+)/) {

        $firstNum = $1;
        @seconds  = split(/\,/,$2);

        foreach $following (@seconds) {

            $Pairs->{$firstNum}{$following} = 1;
        }

        foreach $first (sort {$a <=> $b} keys %Pairs) {

            print "$first\n";
            %second = {$Pairs{$first}};

            foreach $secondID (sort {$a <=> $b} keys %second) {

                print "$first $secondID\n";
            }
        }
        print Dumper($Pairs);
    }
    else {

        print "ERROR\n";
    }
}

Later on, given a pair of numbers I would like to look up to see whether $Pairs{$num1}{$num2} is defined. would I write

if(defined $Pairs{$num1}{$num2})

Or should I check the first key first. Then check the second key

if (defined $Pairs{$num1}) {

    $temp = $Pairs{$num1};
    if (defined $temp{$num2}) {

        print "true\n;
    }
}
my %hash;
while ( <> ) {
  my @numbers = split /\D+/;
  my $key     = shift @numbers;
  @{$hash{$key}}{ @numbers } = ( 1 ) x @numbers;
}


# test it this way...
if ( $hash{ $num1 }{ $num2 } ) { 

}

使用:

%second = %{$Pairs->{$first}};

You have a couple of errors. Firstly you seem to be unsure whether you are using %Pairs or $Pairs to store your hash, and secondly you have %second = {$Pairs{$first}} , which tries to assign a hash reference to the hash %second . Presumably you want my %second = %{ $Pairs{$first} } .

You should always use strict and use warnings at the start of all your Perl programs, and declare all variables at the point of first use using my . This will alert you to simple mistakes you could otherwise easily overlook, and would have shown up your use of both %Pairs and $Pairs in this program, as well as your attempt to assign a single value (a hash reference) to a hash.

Rather than copying the entire hash, you should save a reference to it in $seconds . Then you can dereference it in the following for loop.

Experienced Perl programmers would also thank you for using lower-case plus underscore for local ( my ) variables, and reserving capitals for package and class names.

This program works as you intended, and expects the file name as a command-line parameter:

use strict;
use warnings;

my %pairs;

while (<>) {

  unless ( /(\d+)\s+(.+)/ ) {
    print "ERROR\n";
    next;
  }

  my $first_num = $1;
  my @seconds = split /,/, $2;

  foreach my $following (@seconds) {
    $pairs{$first_num}{$following} = 1;
  }

  foreach my $first (sort { $a <=> $b } keys %pairs) {
    print "$first\n";
    my $second = $pairs{$first};
    foreach my $second_id (sort { $a <=> $b } keys %$second) {
      print "$first $second_id\n";
    }
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM