Results 1 to 10 of 11
Hello al
I am working on a BioInformatics assignment and I am using Perl to parse a few input files. I am new to Perl. Here is my question:
I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 03-16-2003 #1Just Joined!
- Join Date
- Jan 2003
- Posts
- 18
Empty lines in a files using Perl
Hello al
I am working on a BioInformatics assignment and I am using Perl to parse a few input files. I am new to Perl. Here is my question:
I am using this to parse the lines:
($ID, $Rest) = split ( " ", $_, 2);
The first variable is an ID and the rest of the line is a description.
My problem is when a line is blank or empty. My program terminates giving this error:
use of unitialized string in concatenation . or string a line 33.
I get one of these for every line that is blank. Can anyone tell me how to just ignore blank lines?
Thanks
- 03-16-2003 #2Linux Newbie
- Join Date
- Aug 2001
- Location
- USA, Texas
- Posts
- 217
Try something like this:
$someVar .= $Rest if($Rest);
This is as much as I can tell you without a little more of the code you are using.[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
/* Don\'t make me use it! */
- 03-16-2003 #3Just Joined!
- Join Date
- Jan 2003
- Posts
- 18
I'm sorry. The input file is a little different than I described earlier.
The lines in the file look like this:
1.1.1.1 00010 00071 00120 00350 00561
I want to create an output file like so:
1.1.1.1 00010
1.1.1.1 00071
1.1.1.1 00120
1.1.1.1 00350
1.1.1.1 00561
Here is the code that I am using:
#!/usr/bin/perl -w
use FileHandle;
print "Type the name of the input file: ";
$in_file = <STDIN>;
chomp ($in_file);
$_ = $in_file;
$dot_index = rindex($in_file, ".");
$out_file = substr($in_file, 0, $dot_index) . ".dat";
open(INFILE, $in_file) || die "Can't open $in_file: $!";
open(OUT, ">$out_file") || die "Can't create $out_file: $!";
while(<INFILE>) {
chomp;
($ID, $Rest) = split ( ' ', $_, 2) if ($ID);
@maps = split ( ' ', $Rest) if ($ID);
$i = 0;
while ( @maps )
{
print $ID;
print OUT "$ID\t$maps[$i]\n";
$i++;
}
}
close(INFILE) || die "Can't close $in_file: $!";
close(OUT) || die "Can't close $out_file: $!";
I made the changes you suggested. I don't get errors any more, but it doesn't write to the output file. Thanks for the help.
- 03-17-2003 #4Linux Engineer
- Join Date
- Jan 2003
- Location
- Lebanon, pa
- Posts
- 994
I made a few changes to your while loop you might want to look at.
That works correctly on my box with the test line you gave me. If you want, I can probably shorten a few more lines.Code:while(<INFILE>) { chomp; (@line)= split(' ', $_); $len=@line; $i=1; while ($i < $len) { print "$line[0]\n"; print OUT "$line[0]\t$line[$i]\n"; $i++; }
- 03-17-2003 #5Just Joined!
- Join Date
- Jan 2003
- Posts
- 18
That worked. Thank you very much.
One more problem:
The input file has ID numbers that come in the form 'x.x.x.x'
Some of them are full like
1.1.1.1
1.1.1.21
but some come like this:
1.1.1.-
1.-.-.-
I need to disregard the IDs that are not full ( the ones with dashes instead of numbers ).
How do I check for this? I've read about the /d flag, but how do I use it correctly?
Thanks again
- 03-17-2003 #6Linux Engineer
- Join Date
- Jan 2003
- Location
- Lebanon, pa
- Posts
- 994
Revision #2
Code:while(<INFILE>) { chomp; if (!/-/) { (@line)= split(' ', $_); $i=0; while($i < @line) { print "$line[0]\n"; print OUT "$line[0]\t$line[$i]\n"; $i++; } } }
- 03-17-2003 #7Just Joined!
- Join Date
- Jan 2003
- Posts
- 18
You are a life saver. Thank you.
One more question and I'll leave you alone
In the input file, some lines look like this:
1.1.1.10 00040
When I run the script, the second element in the array is ' 00040'. I need it to be '00040'.
How do I fix this?
Thanks again
- 03-17-2003 #8Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
Try (@line) = split(' +', $_);
- 03-17-2003 #9Linux Engineer
- Join Date
- Jan 2003
- Location
- Lebanon, pa
- Posts
- 994
Revision #3. The foreach line should run through the @line array and remove any white space in an element. Though I am not sure how you were getting spaces since we were using ' ' to split up the line.
Code:while(<INFILE>) { chomp; if(! /-/) { (@line)= split(' ', $_); $i=1; foreach (@line) {s/ //;} while($i < @line) { print "$line[0]\n"; print OUT "$line[0]\t$line[$i]\n"; $i++; } } }
- 03-18-2003 #10Just Joined!
- Join Date
- Jan 2003
- Posts
- 18
Hey guys
Thanks a lot for all of your help. I've almost got it. The revision you sent ,genlee, that contained:
foreach (@line) {s/ //};
did not work so I still have that problem. I suppose that it means to replace all white space in a value with nothing. I don't know why it doesn't work, but I still get variables with white space like
' 00010'.
I'm still working on it. If you have any other suggestions, please let me know.
Thanks again


Reply With Quote
