Taylor Lewick wrote:
>
> I have a data file that looks like this...
>
> Date
> Team Name 1
> Team Name 2
> Field1
> Field2
> FieldX
>
> Date
> Team Name 3
> Team Name 4
> Field 1
> FieldX
>
> and so on...
>
> Each entry will have the data and team 1 and 2's name. But the number
> of fields will be variable...
> I would really like to turn this file into something like this...
> Date:Team Name 1:Team Name 2:Field1:Field2:FieldX
> Date:Team Name 3:Team Name 4:Field1:Field2:FieldX
>
> I'm not really sure where to get started. I could load all of the
> values into an array (The file won't be too big) and print them back out
> all separated by colons, then run a regular expression to insert a blank
> line when it detects the date pattern.
>
> I don't know how to go about loading the data into an array or hash, and
> then printing it out formatted on one line until the next date is
> detected. Am not sure how to combine those steps.
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
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>