Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use feature qw(say);
- use strict;
- use warnings;
- {
- my @names; # keeps the same order of the lines
- my %lines; # names and numbers
- read_file('file1', \@names, \%lines);
- read_file('file2', \@names, \%lines);
- for my $name (@names) {
- say $name, " ", join " ", @{$lines{$name}};
- }
- }
- sub read_file {
- my ( $fn, $names, $lines ) = @_;
- open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!";
- while( my $line = <$fh> ) {
- chomp $line;
- next if $line !~ /\S/; # skip empty lines
- my ( $name, $number ) = $line =~ /^(.*)\s+(\d+)\s*$/;
- push @$names, $name if !exists $lines->{$name};
- push @{$lines->{$name}}, $number;
- }
- close $fh;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement