Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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


Comment on Re^3: Combining multiple lines based on the given condition
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1058856]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2014-09-18 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (108 votes), past polls