in reply to rename files dynamically

my @directory = $all_directories;

Where are you intitlizing $all_directories?

push @sourcefiles, $_ if -f and /\.htm*/ ;

That regex is safer (and possibly faster) if you wrote it as /\.html?\z/. As written, it will match ".htmmmmm" and even ".ht", which I don't think is what you want. The '*' matches the last character zero or more times (in your case, 'm'). I think what you meant to use was /\.htm.*/, which says "match '.htm' and then any character zero or more times". But even that isn't as good as the one suggested above.

In the suggested regex (/\.html?\z/), we are using the '?' modifier (which says "match the last character zero or one times") to make the 'l' optional. The '\z' anchors the match to the end of the string (so it won't match 'file.htm.txt', for instance).

----
Reinvent a rounder wheel.

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: rename files dynamically
by Anonymous Monk on Feb 24, 2003 at 16:39 UTC
    I appreciate your response. I define $all_directories in another sub routine. I'm passing the vairable to the sub routine that it is going to find, copy and rename the files. I'm not familiar with passing variables between sub routines but I'll appreciate your suggestions.

      In Perl, you need to explicity pull variables off of @_ when passing params in:

      sub arg_test1 { my $arg1 = shift; # shift() defaults to @_ my $arg2 = shift; return $arg1; } # Another way sub arg_test2 { my ($arg1, $arg2) = @_; return $arg1; } my $test1 = arg_test1(1, 2); my $test2 = arg_test2(1, 2);

      ----
      Reinvent a rounder wheel.

      Note: All code is untested, unless otherwise stated

        Perhaps you may be able to help me. I read the example that you send me but I still not clear on how to do it. it seems that I may have a brain cramp. Basically it try to pass an @array variable to another variable to be process. Here is the rest of my code
        #! perl -w use strict; my $infile = 'c:/doclist1.chr'; my $outfile = 'c:/doclist1.txt'; open IN, "<$infile" or die "Couldn't open $infile, $!"; open OUT, ">$outfile" or die "Couldn't open $outfile, $!"; while(<IN>) { chomp; my @fields = split /,/; my $path_str = $fields[6]; do { warn "Empty field 7"; next } unless $path_str; my @path = split /\\/, $path_str; # assuming you want to remove a few directories my @new_path = join "\\", @path[0,5,6]; print OUT "\n@new_path"; } exit;
        I'm trying to pass @new_path to a $_ variable to be process could you please help?