Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving data into files

Status
Not open for further replies.

smokinace

Programmer
Jun 12, 2008
18
US
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?
 
this looks like 4 characters to me:

$dir = "D".substr( $y, 3, 3);

so whats the problem? Is it not "D001"?






------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
it makes the directory. makes a file by the right name, but only copies the first line of text.
it then makes another directory with the second line of text containing a file with the second line of text and so forth until it dies a few files later
 
read only the first line of the file to create the dir/file, then loop through the rest of the file to copy the data into it.

Code:
open STDIN, $source or die "Can't read source file $source: $! \n";

$first_line = <STDIN>;

#here do your dir/file creation stuff

#now loop through  the rest of the file:

while ($line = <STDIN>) { 
   print $filenew $line;
}




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top