Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Can I use a regex here?

by mndoci (Scribe)
on Apr 17, 2001 at 01:19 UTC ( [id://72955]=perlquestion: print w/replies, xml ) Need Help??

mndoci has asked for the wisdom of the Perl Monks concerning the following question:

Hi all monastery dwellers
I have a quick question. I have a bunch of the following declarations

$LOG_FILE_1 = "pair1" . $RUN_NAME . ".log";
$LOG_FILE_2 = "pair2" . $RUN_NAME . ".log";

and so on
I want to do the following for each of these log files

rename $LOG_FILE_1, "$OUT_DIR/"."allfiles/"."$LOG_FILE_1";

Instead of 10 rename statements (for 10 log files), is there a quick way to do this (I presume using a regex)?
Thanks a ton
mndoci

"What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?" - Sherlock Holmes in 'A study in scarlet'

Replies are listed 'Best First'.
Re: Can I use a regex here?
by Masem (Monsignor) on Apr 17, 2001 at 01:24 UTC
    If you put your log file names into an array, you can use map to do this:
    @old_log_files = [ $LOG_FILE_1, $LOG_FILE_2, ...]; @new_log_files = map { "$OUT_DIR/allfiles/$_" } @old_log_files; for (i=0;$i<scalar @old_log_files; $i++) { rename $old_log_files[$i], $new_log_files[$i]; }

    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Leave the C-Style for loops for C:
      for (0..$#old_log_files) { rename $old_log_files[$_], $new_log_files[$_]; }
      So much nicer, and easy on the hands. :)

      Jeff

      R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
      L-L--L-L--L-L--L-L--L-L--L-L--L-L--
      
Re (PotPieMan): Can I use a regex here?
by PotPieMan (Hermit) on Apr 17, 2001 at 01:29 UTC
    While Masem's suggestion should work, you could do it more simply with loop. For example:
    for my $number (1 .. $END) { my $logFile = 'pair' . $number . $RUN_NAME . '.log'; rename $logFile, "$OUT_DIR/allfiles/$logFile"; }

    (You could skip the "my $number" for idiomatic purposes and use $_ instead.)

    --PotPieMan

Re: Can I use a regex here?
by I0 (Priest) on Apr 17, 2001 at 03:31 UTC
    rename $_, "$OUT_DIR/allfiles/$_" foreach( $LOG_FILE_1, $LOG_FILE_2 );
Re: Can I use a regex here?
by traveler (Parson) on Apr 17, 2001 at 02:10 UTC
    If I understand the problem correctly, what about
    map { rename "pair${_}" . $RUN_NAME . ".log" "$OUT_DIR/"."allfiles/". +"$LOG_FILE_${_}" } (1..10);
    Of course, you may be able to simplify the strings a bit.

    --traveler

Re: Can I use a regex here?
by repson (Chaplain) on Apr 17, 2001 at 05:49 UTC
    Since the filenames follow a pattern, if the number of files is indeterminate or you perfer not to use the individual declarations shown above, you could do something like this:

    rename $_, $OUT_DIR . "/allfiles/$_" for (glob 'pair*' . $RUN_NAME . '.log');

    Alternately you could use opendir and readdir with a regex to check the filename instead using of shell-like globbing.

      This sounds good. My thinking is that since there is a pattern, I would like to use that in some way. Most of the suggestions to my earlier post sound good. However, if your suggestion works, it would be perfect.
      Thanks
      mndoci

      "What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://72955]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-16 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found