Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^3: Combining multiple lines based on the given condition

by 2teez (Priest)
on Oct 19, 2013 at 02:38 UTC ( #1058856=note: print w/replies, xml ) Need Help??

in reply to Re^2: Combining multiple lines based on the given condition
in thread Combining multiple lines based on the given condition

One thing is certain as mentioned by other perlmonks before now that one need know how the OP input file is formatted.
Please see if the below codes gives some light.
Using the OP dataset just as was given and using perl split like thus:

use warnings; use strict; my $l = 'abc,2,3,4,5,6 abc,7,5,2,1,6,2,3 abc,8,2,1,3,1,4 ....... def,8 +,9,4,5,6 def,5,6,2,1 '; my %hash; my $k; for (split/[,. ]+/ => $l){ if(/\D+?/){ $k = $_ }else{ push @{$hash{$k}},$_ } } print $_,',',join (',' => @{$hash{$_}}),$/ for sort keys %hash;
Secondly, since your input is a CSV file, it is better you use module like Text::CSV or Text::CSV_XS.
See an example below with a modified OP dataset.
#!/usr/bin/perl use warnings; use strict; use Text::CSV; use Data::Dumper; my %hash; my $csv = Text::CSV->new( { binary => 1 } ) or die "can't use CSV" . Text::CSV->error_diag(); while ( my $row = $csv->getline(*DATA) ) { push @{ $hash{ shift @$row } }, @$row; # as previously mentioned + by Eily } $csv->eof or $csv->error_diag(); print Dumper \%hash; __DATA__ abc,2,3,4,5,6 abc,7,5,2,1,6,2,3 abc,8,2,1,3,1,4 def,8,9,4,5,6 def,5,6,2,1

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1058856]
[Perl300]: ok, thank you Corion
[Perl300]: I was researching few modules and was not sure about what that date indicates

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2017-12-11 18:07 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (310 votes). Check out past polls.