On 07/13/2013 02:55 AM, John Delacour wrote:

#!/usr/bin/perl
use strict;
my @items;
while (<>) { # or use a file handle
     chomp;
     next if /^start/;
     if (/^end/) {
         print join ", ", @items;
         print "\n";
         undef @items;

that is the wrong way to clear an array (or hash). just assign () to it. by calling undef you also deallocate any storage in the array which perl would normally reuse.

and instead of a one liner this does the same but easier to read and maintain (untested):

use File::Slurp ;

my $text = read_file( shift @ARGV ) ;

$text = s{                      # extended regex syntax enabled by /x
                ^               # beginning of line. internal line
                                # match enabled by /m
                start\n
                (.+?)           # grab record body
                                # .matches \n enabled by /s
                \nend\n
        }
        {       # replace with code enabled by /e
                join ', ', split /\n/, $1 ;
        }gmsex ;                # repeat for all records enabled by /g

print $text ;

uri

--
Uri Guttman - The Perl Hunter
The Best Perl Jobs, The Best Perl Hackers
http://PerlHunter.com

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to