grubmaker
asked on
problems trying to cycle through files and modify them
Hi
I'm writing a script to cycle through the files in a directory and modify if an update file contains certain information. The problems im having (well, the problem im up to) is that i cant seem to extract the first line of the files to update in order to extract a number i need to use to match with the update file. My print debug line is doing nothing. I seem to be able to cycle and open the files fine - its just that i cant seem to be able to do anything with them. Heres the code:
#!/usr/bin/perl
use strict;
my $direc = "/home/jgs/blastportal/";
my $prefix = "taxGI_";
print("Update?: ");
my $in = <STDIN>;
chomp($in);
if($in eq "y"){
my $update = $direc. "gi_taxid_nucl_diff.dmp";
open(UPDLIST, $update) || die("Canont open update list.");
opendir(DIR, $direc);
my @files = grep(/($prefix)/,readdir(D IR));
closedir(DIR);
foreach my $file(@files){
my $path = $direc.$file;
print $path;
open(TAXFILE, "+>> $path") || die("Cannot open a tax file");
my $firstline = <TAXFILE>;
print $firstline;
my $id = ($firstline =~ /\s+\d$/);
while(<UPDLIST>){
print TAXFILE if(/\s+($id)/);
}
close(TAXFILE);
}
close(UPDLIST);
}
I'm new to perl so any advice would be very useful.
thanks
I'm writing a script to cycle through the files in a directory and modify if an update file contains certain information. The problems im having (well, the problem im up to) is that i cant seem to extract the first line of the files to update in order to extract a number i need to use to match with the update file. My print debug line is doing nothing. I seem to be able to cycle and open the files fine - its just that i cant seem to be able to do anything with them. Heres the code:
#!/usr/bin/perl
use strict;
my $direc = "/home/jgs/blastportal/";
my $prefix = "taxGI_";
print("Update?: ");
my $in = <STDIN>;
chomp($in);
if($in eq "y"){
my $update = $direc. "gi_taxid_nucl_diff.dmp";
open(UPDLIST, $update) || die("Canont open update list.");
opendir(DIR, $direc);
my @files = grep(/($prefix)/,readdir(D
closedir(DIR);
foreach my $file(@files){
my $path = $direc.$file;
print $path;
open(TAXFILE, "+>> $path") || die("Cannot open a tax file");
my $firstline = <TAXFILE>;
print $firstline;
my $id = ($firstline =~ /\s+\d$/);
while(<UPDLIST>){
print TAXFILE if(/\s+($id)/);
}
close(TAXFILE);
}
close(UPDLIST);
}
I'm new to perl so any advice would be very useful.
thanks
print $path;
is this your print debug line?
If it is not printing, it means that your grep(/($prefix)/,readdir(D IR)) did not find any matching files. Or perhaps that $in ne "y".
my $id = ($firstline =~ /\s+\d$/);
I'm not sure what you think this is doing, but it sets $id to 1 if $firstline ends with whitespace followed by a single digit.
Based on your later use if $id, I might venture a guess that you might have meant something like:
my($id) = $firstline =~ /\s+(\d+)$/;
is this your print debug line?
If it is not printing, it means that your grep(/($prefix)/,readdir(D
my $id = ($firstline =~ /\s+\d$/);
I'm not sure what you think this is doing, but it sets $id to 1 if $firstline ends with whitespace followed by a single digit.
Based on your later use if $id, I might venture a guess that you might have meant something like:
my($id) = $firstline =~ /\s+(\d+)$/;
ASKER
print $firstline;
was my debug line of choice at the moment. im finding the files, it was the getting first line from each that is an issue. thanks for the regex advice, im going to give it a go now.
was my debug line of choice at the moment. im finding the files, it was the getting first line from each that is an issue. thanks for the regex advice, im going to give it a go now.
ASKER
hmm.. ive tried your corrections and its still only grabbing the 1. the file is simply two columns of numbers, im trying to tell it to grab the first number after the first white spaces...
if the number is not the last thing on the line, then try.
my ($id) = $firstline =~ /\s(\d+)/;
Otherwise, show me $firstline, $id, and your code.
my ($id) = $firstline =~ /\s(\d+)/;
Otherwise, show me $firstline, $id, and your code.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You mentioned that line consists of two number columns ...
my($first_line) = <TAXFILE>;
my($col2) = (split /\s+/, $first_line)[1];
Like wise if you needed column 1:
my($col1) = (split /\s+/, $first_line)[0];
Looking for both columns to make any evaluations
my($col1, $col2) = (split /\s+/, $first_line);
my($first_line) = <TAXFILE>;
my($col2) = (split /\s+/, $first_line)[1];
Like wise if you needed column 1:
my($col1) = (split /\s+/, $first_line)[0];
Looking for both columns to make any evaluations
my($col1, $col2) = (split /\s+/, $first_line);
ASKER
thanks...
yes i was omitting the brackets around $id, my ($id)..... what does this do?
thanks for the assistance
yes i was omitting the brackets around $id, my ($id)..... what does this do?
thanks for the assistance
the parenthesis in
($id) =
puts the assignmet in list context.
In list context, m// returns a list consisting of the subexpressions matched by the parentheses in the pattern, i.e., ($1, $2, $3...)
In scalar context, it returns true if it succeeds, false if it fails.
($id) =
puts the assignmet in list context.
In list context, m// returns a list consisting of the subexpressions matched by the parentheses in the pattern, i.e., ($1, $2, $3...)
In scalar context, it returns true if it succeeds, false if it fails.
ASKER
ahah - great thanks alot
ASKER