could i use local $/ instead of $INPUT_RECORD_SEPARATOR
On Fri, Jul 12, 2013 at 5:33 PM, Shawn H Corey <shawnhco...@gmail.com>wrote: > On Fri, 12 Jul 2013 17:14:42 +0530 > Agnello George <agnello.dso...@gmail.com> wrote: > > > 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 > > > > i came up with this , is there a better way to do this : > > I love record with a definite record terminator. Try: > > #!/usr/bin/env perl > > use strict; > use warnings; > > # -------------------------------------- > > use charnames qw( :full :short ); > use English qw( -no_match_vars ); # Avoids regex performance penalty > > # -------------------------------------- > sub read_record { > my $record_fh = shift @_; > > # all records are from "start\n" to "end\n" > local $INPUT_RECORD_SEPARATOR = "end\n"; > my $record = <$record_fh>; > > # $record undef at end of file > if( defined $record ){ > > # removes "end\n" > chomp $record; > > # removes "start\n" > $record =~ s{ \A start \s* }{}msx; > } > > return $record; > } > > # -------------------------------------- > while( my $record = read_record( *DATA )){ > > # remove trailing "\n" > chomp $record; > > # replace field separators with commas > $record =~ s{ \n+ }{,}gmsx; > > # display the record > print "$record\n"; > } > > > > __DATA__ > 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 > > > -- > Don't stop where the ink does. > Shawn > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > -- Regards Agnello D'souza “Happiness is not so much in having , but in sharing. We make a living by what we get, but we make a Life by what we Give.”