Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


problems trying to cycle through files and modify them

Posted on 2004-10-26
Medium Priority
Last Modified: 2010-03-05

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:

use strict;
my $direc = "/home/jgs/blastportal/";
my $prefix = "taxGI_";

print("Update?: ");
my $in = <STDIN>;

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(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$/);
      print TAXFILE if(/\s+($id)/);


I'm new to perl so any advice would be very useful.

Question by:grubmaker
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4

Author Comment

ID: 12418991
ah, sorry, its something to do with how im opening the TAXFILE. if i open it with < i can read it fine. but i cant write to it. i thought +> and +>> had both read and write access?
LVL 84

Expert Comment

ID: 12419035
   print $path;
is this your print debug line?
If it is not printing, it means that your  grep(/($prefix)/,readdir(DIR)) 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+)$/;

Author Comment

ID: 12419059
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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 12419087
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...
LVL 84

Expert Comment

ID: 12419181
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.
LVL 84

Accepted Solution

ozo earned 2000 total points
ID: 12419216
you may also want
   print TAXFILE if(/\s+($id)\b/);
so that "  3456"  does not match $id="34"

Expert Comment

ID: 12419313
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);


Author Comment

ID: 12429973

yes i was omitting the brackets around $id, my ($id)..... what does this do?

thanks for the assistance
LVL 84

Expert Comment

ID: 12430320
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.

Author Comment

ID: 12430354
ahah - great thanks alot

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question