Taylor Lewick wrote:
> Hi all. Need some help.
>
> 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...
are entries always separate by an empty line?
>
> 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.
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)
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>