For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > Creating a delimited file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Creating a delimited file
Akhenaten

2006-10-12, 6:59 pm

I have a file that has numerous records.

I need to a) delete the blank lines and b) create a delimited file by
labeling the lines

For example

Original File
**************
data
data
data
data

New File
**************

row1:data
row2:data
row3:data
row4:data

Deleting the blanks is a no brainer - how do I label the rows in the
file?

usenet@DavidFilmer.com

2006-10-12, 6:59 pm

Akhenaten wrote:
> Deleting the blanks is a no brainer - how do I label the rows in the file?


You need to keep track of how many lines you've written, such as:

my $line = 0;
while (<$old_file_handle> ) {
print $new_file_handle "row@{[++$line]}:$_"
unless /^\s*\n$/;
}

Do $line++ if you want row numbering to begin at zero instead of one.

--
David Filmer (http://DavidFilmer.com)

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com