in reply to Doubts group data
Hello leoberbert,
As the monks already stated, we do not know exactly what you are trying to do. It would help us a lot to help you coming up with a solution to your problem if you provided us some steps/description etc.
Well I put together a simple script on how I would approach your problem, but I am not sure about the calculation that you are doing with the numbers so I left them unchanged. I am sure that you can make the calculation your self and update the code.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $path_to_file = 'test.txt'; open my $fh, '<', $path_to_file or die "Could not open " . $path_to_file . " $!\n"; chomp(my @lines = <$fh>); close $fh or die "Could not close " . $path_to_file . " $!\n"; # Remove empty lines if this is desired? @lines = grep /\S/, @lines; my %HoA; foreach my $line (@lines) { my ($match, $remaining) = split(/\|\|\|/, $line); push (@{$HoA{$match}}, $remaining); } print Dumper \%HoA; my @updated_lines; foreach my $key (keys %HoA) { my $concat_str; foreach my $i ( 0 .. $#{ $HoA{$key} } ) { if ($i == 0){ $concat_str .= $HoA{$key}[$i] . "; "; } $concat_str .= $HoA{$key}[$i] . " "; } # Trim white space on right $concat_str =~ s/\s+$//; push @updated_lines, $key.$concat_str.'|||'; } print Dumper \@updated_lines; __END__ $ perl test.pl $VAR1 = { '21997' => [ '70049,,20170428154818,20170527235959', '70070,,20170428154739,20170527235959' ], '21998' => [ '70049,,20170428154818,20170527235959', '70070,,20170428154739,20170527235959', '70071,,20170428154739,20170527235959' ] }; $VAR1 = [ '2199770049,,20170428154818,20170527235959; 70049,,201704281 +54818,20170527235959 70070,,20170428154739,20170527235959|||', '2199870049,,20170428154818,20170527235959; 70049,,201704281 +54818,20170527235959 70070,,20170428154739,20170527235959 70071,,2017 +0428154739,20170527235959|||' ];
Update: Sorry because of rush I did not resolve correctly the output. See new code bellow:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $path_to_file = 'test.txt'; open my $fh, '<', $path_to_file or die "Could not open " . $path_to_file . " $!\n"; chomp(my @lines = <$fh>); close $fh or die "Could not close " . $path_to_file . " $!\n"; # Remove empty lines if this is desired? @lines = grep /\S/, @lines; my %HoA; foreach my $line (@lines) { my ($match, $remaining) = split(/\|\|\|/, $line); push (@{$HoA{$match}}, $remaining); } print Dumper \%HoA; my @updated_lines; foreach my $key (keys %HoA) { my $concat_str; foreach my $i ( 0 .. $#{ $HoA{$key} } ) { if ($i == 0){ $concat_str .= $HoA{$key}[$i] . "; "; } else { $concat_str .= $HoA{$key}[$i] . " "; } } # Trim white space on right $concat_str =~ s/\s+$//; my $final_str = join ('', $key,'|||' , $concat_str, '|||'); push @updated_lines, $final; } print Dumper \@updated_lines; __END__ $ perl test.pl $VAR1 = { '21997' => [ '70049,,20170428154818,20170527235959', '70070,,20170428154739,20170527235959' ], '21998' => [ '70049,,20170428154818,20170527235959', '70070,,20170428154739,20170527235959', '70071,,20170428154739,20170527235959' ] }; $VAR1 = [ '21997|||70049,,20170428154818,20170527235959; 70070,,201704 +28154739,20170527235959|||', '21998|||70049,,20170428154818,20170527235959; 70070,,201704 +28154739,20170527235959 70071,,20170428154739,20170527235959|||' ];
Hope this helps.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Doubts group data
by leoberbert (Novice) on May 04, 2017 at 15:35 UTC |