GiJoe has asked for the wisdom of the Perl Monks concerning the following question:
I have a text file that looks like this:
0.000000 #need to delete this line
0.000000 0.000000 0.000000 ..256 elements
0.000000 0.000000 0.000000 17.962402 10.630810 ..256 elements
repeats....
Question1: How can I separate every other 256 element into a new text file and leave a blank line or space between each 256 element?
Question2: How do I get rid of that pesky first line at the beginning of the text file: "0.000000 "
Here is how I started:
#!/usr/local/bin/perl open (OLD, 'old.txt'); open (NEW, '>new.txt'); while (<OLD>) { chomp; next unless $_ =~ /\t/ && /0.000000/; next unless $_ !~ "F"; $_ =~ s/\t//; print NEW ("$_\n"); } close (OLD); close (NEW);
Any feedback is greatly appreciated:-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Text Massage
by citromatik (Curate) on Apr 07, 2009 at 23:21 UTC | |
This is what I understand from your explanation: This is some (untested) code to do this:
I am asuming that the records are separated by tabs. The first line can also be discarded inside the while loop by using the $. variable (see perlvar: next unless $. == 1). If I haven't understand you correctly, please let me know citromatik | [reply] [d/l] [select] |
|
Re: Text Massage
by shmem (Chancellor) on Apr 07, 2009 at 23:21 UTC | |
To get rid if the first line shoved through a file handle:
As for question number 1: count them. There's split for splitting a string into a list, which you can stuff into an array. See push. Keep tack of the number of elements. Evaluating an array in scalar context gives you the number of elements in an array. See perldata. Really, read all of the perl man-pages from top to bottom. Don't despair if you don't grab all things in there at first. Just read them. Things will assemble; ruminating they fall into place. | [reply] [d/l] |
|
Re: Text Massage
by Marshall (Canon) on Apr 08, 2009 at 00:26 UTC | |
Can you explain Question #1 further? For something like this make a few rows with say 6 numbers each (like 0.0 12.1 6.5 2.3,etc...no need for full precision in the example). Show that input and then show desired output. | [reply] |
by Anonymous Monk on Apr 08, 2009 at 04:19 UTC | |
What I have: 0.000000 #delete this line 0.000000 0.000000 0.000000 ..curve 1 256 elements 0.000000 0.000000 0.000000 ..curve 2 256 elements 0.000000 2.566058 1.466319 ..curve 1 256 elements 2.199478 2.199478 3.665797 ..curve 2 256 elements pattern continues curve 1,2,1,2,1,2...
What I need:
output to text file "B"
Curve2: LBIN
Frame readOk!!!
Curve2: LBIN | [reply] |
by Marshall (Canon) on Apr 08, 2009 at 07:43 UTC | |
data attached...So what else needs to happen? Read more... (17 kB)
| [reply] [d/l] [select] |
by GiJoe (Initiate) on Apr 08, 2009 at 22:08 UTC | |
by GiJoe (Initiate) on Apr 09, 2009 at 14:05 UTC | |
by Marshall (Canon) on Apr 14, 2009 at 12:30 UTC | |