Data File, turn fields on mulitple lines into records on one line.. .

Data File, turn fields on mulitple lines into records on one line.. .

Post by dzhu » Sat, 17 Jan 2004 07:57:08



are entries always separate by an empty line?


you don't need to load everything into an array or hash. you can read and
process one entry at a time. assuming the entries are separated by an empty
line, the following works well:

#!/usr/bin/perl -w
use strict;

$/ = "\n\n";

while(<DATA>){

s/\n/:/g; s/:+$//;

print "$_\n";
}

__DATA__
Date
Team Name 1
Team Name 2
Field1
Field2
FieldX

Date
Team Name 3
Team Name 4
Field 1
FieldX

__END__

prints:

Date:Team Name 1:Team Name 2:Field1:Field2:FieldX
Date:Team Name 3:Team Name 4:Field 1:FieldX

david
--
sub'_{print"@_ ";* \ = * __ ,\ & \}
sub'__{print"@_ ";* \ = * ___ ,\ & \}
sub'___{print"@_ ";* \ = * ____ ,\ & \}
sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)
 
 
 

Data File, turn fields on mulitple lines into records on one line.. .

Post by rob » Sat, 17 Jan 2004 22:04:32


Hi Taylor.

If you set the input record separator - $/ - to a null string then it will
split the input file on one or more blank lines.

Take a look at the program below.

HTH,

Rob


#!perl
use strict;
use warnings;

local $/ = '';

while (<DATA>) {
my @data = split /\n/;
print join(':', @data), "\n";
}

__DATA__
Date
Team Name 1
Team Name 2
Field1
Field2
FieldX

Date
Team Name 3
Team Name 4
Field 1
FieldX

**OUTPUT

Date:Team Name 1:Team Name 2:Field1:Field2:FieldX
Date:Team Name 3:Team Name 4:Field 1:FieldX