i am having problems with breaking a file into several files.
here is my code.
my $source = $ARGV [0];
my $i = 0;
open STDIN, $source or die "Can't read source file $source: $! \n";
while ($line = <STDIN>)
{
$x = substr( $line, 3, 1);
if ($x =~ /^|/){
$y = substr( $line, 18, 13);
$i++;
$fileold = $filenew;
$filenew = "F".$i;
if ($i > 1) {
close $fileold;
}
$dir = "D".substr( $y, 3, 3);
#open $filenew, "test.txt" or die "Couldn't open\n";
unless(-d $dir){ #checks for existing directory
mkdir $dir or die "im not dead\n";
}
open $filenew, ">$dir/$y" or die "Couldn't open\n";
print $filenew "$line";
}
else {
print $filenew "$line";
}
}
close $filenew;
close (STDIN);
this program should take this data
>gi|155369268|ref|NM_001100917.1| Homo sapiens tetraspanin 19 (TSPAN19), mRNA
"body"
put it in a directory called D001, make a file called "NM_001100917.1", and then put the data in the file.
right now it makes the first line correct but puts the data in directories of 4 characters. cna anyone help?
here is my code.
my $source = $ARGV [0];
my $i = 0;
open STDIN, $source or die "Can't read source file $source: $! \n";
while ($line = <STDIN>)
{
$x = substr( $line, 3, 1);
if ($x =~ /^|/){
$y = substr( $line, 18, 13);
$i++;
$fileold = $filenew;
$filenew = "F".$i;
if ($i > 1) {
close $fileold;
}
$dir = "D".substr( $y, 3, 3);
#open $filenew, "test.txt" or die "Couldn't open\n";
unless(-d $dir){ #checks for existing directory
mkdir $dir or die "im not dead\n";
}
open $filenew, ">$dir/$y" or die "Couldn't open\n";
print $filenew "$line";
}
else {
print $filenew "$line";
}
}
close $filenew;
close (STDIN);
this program should take this data
>gi|155369268|ref|NM_001100917.1| Homo sapiens tetraspanin 19 (TSPAN19), mRNA
"body"
put it in a directory called D001, make a file called "NM_001100917.1", and then put the data in the file.
right now it makes the first line correct but puts the data in directories of 4 characters. cna anyone help?