Beginner working with tab-delimited text file

Reply

Join Date: Dec 2006
Posts: 7
Reputation: brax4444 is an unknown quantity at this point 
Solved Threads: 0
brax4444 brax4444 is offline Offline
Newbie Poster

Beginner working with tab-delimited text file

 
0
  #1
Dec 18th, 2006
Hello all,
I'm trying to read in an entire tab-delimited files with the following data layout:

File Title Line is always at the top.
DataName1 DataNam2 DataName3
12345 123.4 12.456
9876 987.65 45678

There may be any number of rows and columns after the title line.

I want to read in the file and output separate files for each column. For instance:

DataName1.txt would contain:
12345
9876

DataName2.txt would contain:
123.4
987.65

DataName3.txt would contain:
12.456
45678


I think Perl may be best for this specific problem though I could also use C++. Anyone have an easy solution they wouldn't mind sharing?

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 262
Reputation: Dukane is an unknown quantity at this point 
Solved Threads: 22
Dukane's Avatar
Dukane Dukane is offline Offline
Posting Whiz in Training

Re: Beginner working with tab-delimited text file

 
0
  #2
Dec 18th, 2006
Open the first file, then read each line into an array. Pop off the top 2 lines, as they are not data useful for the program.

For each line in the file:
Read the first column, then add it to an array. You can do this by using split();
Read the second column then add it to a second array.
Read the third column then add it to a third array.

Close the file.

Open a new file, for the first column.
Print the whole array into the file.
Close the file.

Open a second file, for the second column.
Print the whole array into the second file.
Close the file.

Open a third and last file, for the last column.
Print the whole array into the third file.
Close the file.

Perl script is complete.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Beginner working with tab-delimited text file

 
0
  #3
Dec 18th, 2006
brax4444,

what perl code have you tried so far? Is this class/school work?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 7
Reputation: brax4444 is an unknown quantity at this point 
Solved Threads: 0
brax4444 brax4444 is offline Offline
Newbie Poster

Re: Beginner working with tab-delimited text file

 
0
  #4
Dec 18th, 2006
I was hoping for sample that would solve this simple case so that I might have a framework to look at when I solve the real problem. I'm basically looking for a good example of Perl code to handle this type of thing. I'll paste what I have so far tomorrow. Thanks.


School? No; school isn't in session anyway.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Beginner working with tab-delimited text file

 
0
  #5
Dec 19th, 2006
Well then, here is one possble way:

  1. use strict;
  2. use warnings;
  3. use IO::File;
  4. open FH,'datafile.txt' or die "$!";
  5. my @files = split(/\s+/,<FH>);
  6. for (@files) {
  7. my $f = "$_.txt";
  8. $_ = new IO::File "> $f" or die "$!";
  9. }
  10. while(my @data = split(/\s+/,<FH>)) {
  11. for (@files) {
  12. print $_ shift @data,"\n";
  13. }
  14. }
  15. for (@files) {
  16. close->$_;
  17. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 7
Reputation: brax4444 is an unknown quantity at this point 
Solved Threads: 0
brax4444 brax4444 is offline Offline
Newbie Poster

Re: Beginner working with tab-delimited text file

 
0
  #6
Dec 19th, 2006
Thank you! I have begun adapting it.

Any idea what this would mean? Perhaps perl is running out of memory? I have 2GB of RAM but don't know what it can allocate for it's own use.

Can't call method "IO::File=GLOB(0x818417c)" without a package or object reference at internetTest.pl line 17, <FH> line 1370.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Beginner working with tab-delimited text file

 
0
  #7
Dec 19th, 2006
let's see your perl code and what is line number 1370 of the file you are working with?

Generally there is no memory allocation necessary for perl scripts. Perl will use what memory is needed all they way to using all memory if thats what it takes.
Last edited by KevinADC; Dec 19th, 2006 at 7:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 7
Reputation: brax4444 is an unknown quantity at this point 
Solved Threads: 0
brax4444 brax4444 is offline Offline
Newbie Poster

Re: Beginner working with tab-delimited text file

 
0
  #8
Dec 20th, 2006
Line 1370 is the last line and it looks exaclty like the rest of them. Perhaps it should have an endline character??

Here is [perlcode] echo "
#!/usr/bin/perl
use strict;
use warnings;
use IO::File;
open FH,'file.txt' or die "$!";
my @files = split(/\s+/,<FH>);
for (@files) {
my $f = "$_.txtt";
$_ = new IO::File "> $f" or die "$!";
}
while(my @data = split(/\s+/,<FH>)) {
for (@files) {
print $_ shift @data,"\n";
}
}
for (@files) {
close->$_;
}
";
[/perlcode]

I believe this is an exact copy of what you gave me. I'm trying to have it start on the second line instead of the first. For now, I just remove the first line in the input file and it works good enough to test.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Beginner working with tab-delimited text file

 
0
  #9
Dec 20th, 2006
OK, so it's working now?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 7
Reputation: brax4444 is an unknown quantity at this point 
Solved Threads: 0
brax4444 brax4444 is offline Offline
Newbie Poster

Re: Beginner working with tab-delimited text file

 
0
  #10
Dec 20th, 2006
No, it always gives an error as shown when it handles the last line in a file, apparently no matter what size.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC