Re: Data File, turn fields on mulitple lines into records on one line.. .
Rob Dixon wrote: 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. #!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 TMTOWTDI :-) #!/usr/bin/perl use strict; use warnings; ( $/, $\ ) = ( '', \n ); while ( DATA ) { chomp; tr/\n/:/; print; } __DATA__ Date Team Name 1 Team Name 2 Field1 Field2 FieldX Date Team Name 3 Team Name 4 Field 1 FieldX John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Data File, turn fields on mulitple lines into records on one line.. .
John W. Krahn wrote: TMTOWTDI :-) #!/usr/bin/perl use strict; use warnings; ( $/, $\ ) = ( '', \n ); while ( DATA ) { chomp; tr/\n/:/; print; } Rogue! /R -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Data File, turn fields on mulitple lines into records on one line.. .
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... 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. Any help is greatly appreciated... -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Data File, turn fields on mulitple lines into records on one line.. .
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