Hi there,
I just started programming with PERL and am trying to put together my first little data manipulation program. I am working on a MAC with OSX.
I have a data file with the following header that has been created on a Windows XP machine:
------ Begin Next Fly'm ------
"Begin Fly'm (Thu Jul 24 01:22:02 2008)"
"Ions Flown Separately, Comp Quality(100)"
"Number of Ions to Fly = 200000"
"Changes:","Mass","Charge","X","Y","Z","KE","Azm", "Elv","Time of Birth"
" ","YES","NO ","NO ","NO ","NO ","YES","YES","YES","NO "
"Ion N","Events","TOF","Mass","X","KE","KE Error"
1,1,0,2.01402,9.53,1,1.28051e-009
1,4,0.725546,2.01402,178,3181.59,0.542952
My goal is to get rid of the header and the empty lines to finally have an output file with only the number entries.
I've put together some lines that -IMO- should work, but they don't and I don't really get it why they don't do their work. - # read file that is given in STDIN
-
$dat = shift or die "Need input file with data. \n";
-
-
# open files for read and for write
-
open DAT,"< $dat" or die "Cannot read $dat\n";
-
open OUT, "> output.txt" or die "Cannot open file!\n";
-
-
-
# loop for reading input file
-
while($dat = <DAT>){
-
- # skip lines beginning with ", - and empty
-
next if $dat =~ /^\s*$|\"|-/;
-
-
# next if $dat =~ /^\"/;
-
# s/^-//;
-
# s/^"//;
-
#s/^\s*//;
-
chomp($dat);
-
-
print OUT <DAT>; #print the contents of the input file into the ouput file
-
}
-
-
# close files for reading and writing
-
close DAT;
-
close OUT;
The bold part is the line where the skipping /erasing should take place. I tried several different combinations of the command, put m/ before the expression and /i after and even had it split into three seperate commands that should skip ", - and whitespace:
for just erasing the " and the same for - and whitespace.
My output file looks like this when I run the program:
"Begin Fly'm (Thu Jul 24 01:22:02 2008)"
"Ions Flown Separately, Comp Quality(100)"
"Number of Ions to Fly = 200000"
"Changes:","Mass","Charge","X","Y","Z","KE","Azm", "Elv","Time of Birth"
" ","YES","NO ","NO ","NO ","NO ","YES","YES","YES","NO "
"Ion N","Events","TOF","Mass","X","KE","KE Error"
1,1,0,2.01402,9.53,1,1.28051e-009
1,4,0.725546,2.01402,178,3181.59,0.542952
There are some empty lines in the beginning and then the usual header minus the very first line.
I even tried to completely erase the header using the commands in line 16 and 17 of my code, but usually I get the following error for that attempt:
Use of uninitialized value in substitution (s///) at ./foo.pl line 16, <DAT> line 1.
Use of uninitialized value in substitution (s///) at ./foo.pl line 17, <DAT> line 1.
The skipping of lines worked to some extent with a simple little textfile I made up (except for getting rid of the empty lines), but it more or less fails when I try the same on my datafile.
Hopefully someone can help me with this problem.
Thanks a lot.
4 7201
I just saw that the very first line of my data file gets erased whenever I run the program. No idea how this can happen.
I just saw that the very first line of my data file gets erased whenever I run the program. No idea how this can happen.
Modify the while() loop as below: -
while($dat = <DAT>){
-
# skip lines beginning with ", - and empty
-
next if $dat =~ /(^\s*$)|(^\")|(^-)/;
-
print OUT $dat; #print the contents of the input file into the ouput file
-
}
-
The regex is changed to suit your need (skip lines beginning with ", - and empty), the regex you used would skip blank lines, lines containing " and - (not just the lines beginning with).
The chomp() line was removed to obtain lines of output. If you want all the lines having numbers in a single line output, you can include that.
The first line of data was getting removed because of this:
When you have already assigned <DAT> to $dat, using <DAT> again will read the next line and print it. You should be using $dat in this line.
- while(my $dat = <DAT>) {
-
next if ($dat =~ /^([-"])|^\s*$/);
-
print OUT $dat;
-
}
Hey thanks very much.
That totally solved my problem. :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sugapablo |
last post by:
I know how to read a text file in ASP, I know how to append lines to a
text file.
How do I simply erase all lines from a text file?
--
http://www.sugapablo.com <--music ]
|
by: eeykay |
last post by:
Hello,
I am facing a starnge problem while erasing the last member in a
vector. I am using VC++ .NET 2002 complier. I have vector of
CComPtr<..> (irrelevant here), and then I iterate over the...
|
by: darrel |
last post by:
I'm backtracking to a problem I had a month or so ago.
I need to write XML files quite a bit. I'm finding that the way I'm doing it
doesn't write a new, clean XML file each time, but just dumps...
|
by: Ivan Liu |
last post by:
I would like know how I can skip a line while reading
a set of input data (from a text file) if the first character
of the line is "#".
My original code reads:
ifstream Infile("data.dat");
...
|
by: lisa.engblom |
last post by:
Hi,
I've just started programming in python, and have run into an
unexpected problem. I am using python to pull text data from some csv
files. I have one file that has the important...
|
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= |
last post by:
I'm drawing text using the DrawString method. I need a way to go back and
erase one or more characters. As a first thought it seemed that one way to
do it would be to go back to the character(s)...
|
by: Anthony1312002 |
last post by:
Hello. I have a scipt the reads and imports a text file into a database table. Below is an example of the text file I'm importing and the script I'm using to accomplish the import. You'll notice at...
|
by: Gustaf |
last post by:
Hi all,
Just for fun, I'm working on a script to count the number of lines in source files. Some lines are auto-generated (by the IDE) and shouldn't be counted. The auto-generated part of files...
|
by: Jerry Coffin |
last post by:
In article <4fae62b0-6858-4e9e-830e-9eecf6691d4a@
59g2000hsb.googlegroups.com>, friend.blah@googlemail.com says...
Each time you read from the file, keep track of the file position after...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |