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

Hello all. i have this little snppet of code here:
#libraries used are up above my @dirs=("C:/jobs/writebacks"); #array containing directories that ar +e being searched foreach my $dir (@dirs) { #look at each directory my @files=read_dir($dir); # read directory $dir =~ /[^:]\/(.*)\/$/; my $end_direct = $1; chdir("$dir"); #change back to the directory just read foreach(@files) { #steps through each file in the contents if ( /^(\d{5})$/) { my $file= $1; #assigns regex to the file name $file = $file."/$end_direct"; DPSI::utils::mkdirR("C:\\FolderB\\$file"); DPSI::utils::nCopy ($_, "C:\\FolderB\\$file"); }#match bracket if statement }#inner for each }#outer
$1 is not receving that value of the reg ex to apply to $end_direct. i am trying to get it read the subdirectory, in this case writebacks and paste it on the end of a new directory, say C:/new_dir/writebacks. anyone have any thoughts why this isn't working?

Replies are listed 'Best First'.
Re: $1 not getting applied to reg ex
by tlm (Prior) on Jul 26, 2005 at 14:11 UTC

    How do you know that the regex has matched? If $dir doesn't end in a slash, the match will fail and will not set $1.

    the lowliest monk

Re: $1 not getting applied to reg ex
by japhy (Canon) on Jul 26, 2005 at 14:56 UTC
    Your regex doesn't match the data you're giving it! Your string is "C:/jobs/writebacks", but your regex says: "match a non-colon character, than a /, then zero or more characters, then a /, then the end of the string". The first problem is that you probably meant [^:]:, which would match a non-colon (like 'C') and then the colon after it. But why not just use the File::Basename module?

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: $1 not getting applied to reg ex
by davorg (Chancellor) on Jul 26, 2005 at 14:10 UTC

    When you previewed your post and you saw the message saying:

    If something looked unlike you expected it to you might need to check out Writeup Formatting Tips

    Did you _really_ like the way your post looked?

    It's hard to read your code with it formatted as badly as it is, but I strongly suspect that you are trying to reinvent File::Find. And you'd probably be better off using the original File::Find instead.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      hi hows it going. this code is actually meant to transfer some directories along with their sub contents to another directory. basically i am looking at 6 or so base directories, of the form say C:/base_dir/5_digit_number/contents. i need to move them to C:/new_folder/5_digit_number/base_dir_name/contents. once i go through the other base directories they will have directories with the same 5 digit numbers, but different contents, and it would add a new directory within 5_digit_number with its contents, and so on so forth. if you have any thoughts on what i am doing incorrectly, please let me know. thanks

        So I was right. You _are_ trying to reinvent File::Find :)

        I think you want something a bit like this (but I haven't had a chance to test it)

        #!/use/bin/perl use strict; use warnings; use File::Find; use File::Path; use File::Copy; @_ >= 2 or die "Usage: $0 <source_dir> <dest_dir>\n"; my ($src, $dest) = @_; find(\&do_this, $src); sub do_this { if (-d) { mkpath([$File::Find::name]); } else { my $new = $File::Find::name; $new =~ s|/$src/|/$dest/|; copy $_, $new; } }
        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: $1 not getting applied to reg ex
by phaylon (Curate) on Jul 26, 2005 at 14:06 UTC
    Please use <code> tags, so ppl can read your source.

    Ordinary morality is for ordinary people. -- Aleister Crowley