> On 12/07/2013 13:44, Agnello George wrote:
>> hi
>>
>> i have raw data that is like this in a flat file .
>>
>> start
>> name:agnello
>> dob:2 april
>> address:123 street
>> end
>> start
>> name:babit
>> dob:13 april
>> address:3 street
>> end
>> start
>> name:ganesh
>> dob:1 april
>> address:23 street
>> end
>>
>>
>> i need to get the data in the following format
>>
>> name:agnello, dob:23 april ,address:123 street
>> name:babit,dob:13 april,address:3 street
>> name:ganesh,dob:1 april,address:23 street
>
> perl -0 -wple 's/^start\n(.*?)\nend\n/$_=$1;y{\n}{ };"$_\n"/emgs'
>
> --
> Ruud
>
>
I love a one-liner as much as the next guy, and this one is very
cool.. but it doesn't produce output exactly like the request (no
commas).
So.. thought I might toss another solution into the mix (why not,
right?) :)
#!/usr/bin/perl
use strict;
use warnings;
my @outvalues;
while (<>) {
chomp;
my $seq = /^start/ .. /^end/;
if ($seq == 1) { # Clear array on "start"
@outvalues = ();
} elsif ( $seq =~ /e0$/i) { # Output a line on "end"
print join(', ', @outvalues), "\n" ;
} else { # Otherwise, save the line
push @outvalues, $_; # in the array
}
}
This probably violates at least 451 "Best Practices", but I like it.
Nathan
--
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/