in reply to Re: Combining multiple lines based on the given condition
in thread Combining multiple lines based on the given condition
Thanks all of you for your replies. Please find the following code.If you have any further changes to this code please let me know .Also, I don't know how to use text::csv , so if anyone could help, it will be nice.
#!usr/bin/perl use strict; open FH, "/home/test1/hello.csv" or die; my @array,@array1; #----------I'm pushing all abc's and def's data into respective arrays +--------------------------- while(<FH>) { if ($_=~/^abc/) { push(@array,$_); } elsif($_=~/^def/) { push(@array1,$_); } } #----------I'm writing only one for loop for abc, it will be same for +def too. ------------------------------ my @hello; foreach my $i(0..$#array-1) { #-------below code: it splits the second abc and and so on. i.e., stri +ps second abc from the digits.------------------- @hello=split /^\w+\,/,$array[$i+1]; foreach my $j(0..$#hello) { #--------below code:this is for replacing \n of first abc,2,3,4,5,6 wi +th a comma ( ex: abc,2,3,4,5,6, ) $array[$i]=~s/\n/,/g; #--------------below code:here it concatenates the data ( data should +look like: 2,3,4,5,6,7,5,2,1,6,2,3,8,2,1,3,1,4 )---------- abc,$array[$i]=$array[$i].$hello[$j]; print $array[$i],"\n"; } } # In my above code, the data is concatenated as follows: abc,2,3,4,5,6 ,7,5,2,1,6,2,3 ,8,2,1,3,1,4 #But expected output is : abc,2,3,4,5,6,7,5,2,1,6,2,3,8,2,1,3,1,4
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Combining multiple lines based on the given condition
by marinersk (Priest) on Oct 18, 2013 at 23:21 UTC | |
|
Re^3: Combining multiple lines based on the given condition
by 2teez (Vicar) on Oct 19, 2013 at 02:38 UTC | |
|
Re^3: Combining multiple lines based on the given condition
by Laurent_R (Canon) on Oct 19, 2013 at 10:34 UTC |