Reputation: 75
I currently have a perl script which appends to a file with the content in this fashion:
a 1
b 2
c 3
a 2
b 3
c 4
a 3
b 4
c 5
and I want the output to be like this:
a 1 2 3
b 2 3 4
c 3 4 5
This is my code which is extracting 2 columns from a file:
open my $file_in, "<", "input_file.txt" or die($!);
open my $file_out, '>', 'output_file.txt' or die $!;
$now_string = localtime;
print "$now_string\n";
while( <$file_in> ) {
next unless $i++;
my @columns = split /\s+/, $_;
print $file_out "$now_string\t$columns[0]\t$columns[2]\n";
}
close $file_in;
close $file_out or die $!;
Any ideas please? I'm using a Solaris 9 machine so can't run perl modules or similarly perl -E, perl -lane. Thanks in advance.
Upvotes: 0
Views: 975
Reputation: 50647
perl -le'
push @{$h{$_->[0]}}, $_->[1] for map [split], <>;
print join " ", $_, @{$h{$_}} for sort keys %h;
' file
perl script.pl input_file.txt > output_file.txt
$\ = "\n";
push @{$h{$_->[0]}}, $_->[1] for map [split], <>;
print join " ", $_, @{$h{$_}} for sort keys %h;
more lengthy (and approximate) version,
$\ = "\n"; # append newline when invoking print
my %h;
# read lines from STDIN
while (<>) {
# split line on white spaces
my @F = split;
# push value of second column into $h{$F[0]} array
push @{ $h{$F[0]} }, $F[1];
}
# sort unique values of first column
for my $k (sort keys %h) {
# print value of first column,
# together with values belonging to it
print join " ", $k, @{ $h{$k} };
}
Upvotes: 1
Reputation: 63
You should put atleast what you have tried so far, not just the question. You can easily do it using hashes.
use Data::Dumper;
use autodie;
# Parse text file
open(FH,$ARGV[0]);
my @array=<FH>;
my %hash;
foreach(@array)
{
$_=~m/(\w+)\s(\w+)/;
push(@{$hash{$1}},$2);
}
print Dumper \%hash;
Upvotes: 0
Reputation: 35198
perl -lane "
push @{$v{shift @F}}, @F;
END { print qq{$_ @{$v{$_}}} for sort keys %v }
" file1.txt
Outputs:
a 1 2 3
b 2 3 4
c 3 4 5
Upvotes: 0
Reputation: 8532
Maybe you want partition_by
from List::UtilsBy
?
use 5.010;
use List::UtilsBy 'partition_by';
my @pairs = (
[ 'a', 1 ],
[ 'b', 2 ],
...
);
my %groups = partition_by { $_->[0] } @pairs;
foreach my $group ( sort keys %groups ) {
say "Group $group: " . join " ", @{$groups{$group}};
}
Upvotes: 1
Reputation: 553
Simple, but it helps to know how you got the data so far - variable?
You need a HashArray. Someting like that:
$x->{'a'} ||= []; # init array
push $x->{'a'}, 1; # add value
...
foreach my $h (keys %$x){
print $h;
foreach my $a (@{$h->{ $h }}){
print $h->{ $h };
}
print "\n";
}
I did not run the code, just hacked it in here.
Upvotes: 0