Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with simple text file conversion

Status
Not open for further replies.

hp3ba

Technical User
Joined
Jul 11, 2007
Messages
4
Location
US
I have a text file in the format:

1,3
1,5
1,4
1,4.3
5,4
54,3
100,1
100,1
1100,2
etc

basically just two columns, comma separated.
I need to convert that into a text file with the format as follows:

1:
5
4
4.3
5:
4
54:
3
100:
1
1
1100:
2
etc

Column one appears once, followed by ':', with all of column two
values after it, in the same order as original file.

I have a program called TextPipe Pro that can handle the conversion, so I don't even need a whole perl script, I just have to fill in the
'perl-style pattern' for Find and 'perl-style pattern' for Replace.
Any help would be greatly appreciated.
 
Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$prevKey[/blue] = [red]'[/red][purple][/purple][red]'[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<DATA>[red])[/red] [red]{[/red]
	[black][b]my[/b][/black] [red]([/red][blue]$field1[/blue], [blue]$field2[/blue][red])[/red] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url] [red]'[/red][purple],[/purple][red]'[/red][red];[/red]
	[olive][b]if[/b][/olive] [red]([/red][blue]$field1[/blue] ne [blue]$prevkey[/blue][red])[/red] [red]{[/red]
		[blue]$prevkey[/blue] = [blue]$field1[/blue][red];[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$field1[/blue]:[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]
	[black][b]print[/b][/black] [red]"[/red][purple][blue]$field2[/blue][/purple][red]"[/red][red];[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]1,3[/teal]
[teal]1,5[/teal]
[teal]1,4[/teal]
[teal]1,4.3[/teal]
[teal]5,4[/teal]
[teal]54,3[/teal]
[teal]100,1[/teal]
[teal]100,1[/teal]
[teal]1100,2[/teal]
 
Thanks MillerH.
Sorry for my perl ignorance, but that seems like the entire program -
just need to save it as a pl file and run it -
how do i tell it to save the result as a txt file?

(Right now if I run it, it doesn't save the print to anywhere.)

Thanks much.
 
Thanks again.
Here's what I ended up with:

Code:
open(INFILE,  "input.txt")   or die
    "Can't open input.txt: $!";
open(OUTFILE, ">output.txt") or die
    "Can't open output.txt: $!";

my $prevKey = '';

while (<INFILE>) {
my ($field1, $field2) = split ',';
if ($field1 ne $prevkey) {
$prevkey = $field1;
print OUTFILE "$field1:\n";
}
print OUTFILE "$field2";
}


Works like a charm.
 
I would suggest closing your file handles, but besides that it looks good.

close(INFILE);
close(OUTFILE);

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Yeah, from what I read (as a newbie to Perl) -
you're supposed to 'close', but Perl is smart
enough to clean up for you.
 
Yes, perl is quite smart and it will do a lot of things for you. However there are two very good reasons why it is important to follow travs69 suggestion.

1) It self-documents your code.

If you are done working with a file handle, the easiest way to communicate that to a future programmer maintaining your code is to explicitly close your file handles. This way you don't have to include a comment about your intentions. It is easily deduced by the code itself.

And believe me, that future programmer could simply be yourself in 6 months time. It doesn't take long before you've forgotten what you were originally thinking when you designed something. Especially if you're in the huge learning curve stage, which you most certainly would be as a newbie.

2) It can be the source of strange bugs in larger systems.

Once you start writing larger programs, or persistent systems, not closing your file handles explicitly can start to be the source of very strange bugs. The easiest way to ensure that a handle is properly flushed is to close it.

Also, closing the file handle can teach you the very good practice if only opening and leaving open those handles that you truly need when you need them. A common practice is writing a sub that will output to a file. You'll sometimes see people open the handle at the beginning of a script and only including print statements within the sub. A much better practice is to open the handle within the sub and the immediately close the handle before returning. This very effectively self-documents what the sub is writing to what the handle is for.

In short, follow travs advice. It takes two seconds to write close(HANDLE). And you'll definitely come to appreciate the practice in the future.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top