in reply to Compare and copy array values

It sounds like you need something like this:

my %file_lookup; for my $file ( @update ) { push @{ $file_lookup{ substr( $file, 0, 4 ) } }, $file; } for my $dir ( @prod ) { next unless exists $file_lookup{ $dir }; for my $file ( @{ $file_lookup{ $dir } } ) { copy( $file, "$dir/$file" ) or warn "Cannot copy '$file' to '$ +dir/$file' $!" } }

Replies are listed 'Best First'.
Re^2: Compare and copy array values
by tgolf4fun (Novice) on Mar 19, 2008 at 20:54 UTC
    If the variables are declared a the beginning of the script do I need to use "my" when referencing them further down in the script?

      If you do re-declare them, they will override the earlier one.

      my $crap =1; print "crap is $crap\n"; my $crap =2; print "crap is $crap\n";
      produces
      crap is 1 crap is 2

      If you include use warnings; at the beginning of the scripts, you will get "my" variable $crap masks earlier declaration in same scope at crap.pl line 8. which can be very useful!

      Just keep in mind that it checks in the same scope. That means you can re-declare the same variable name inside a loop and Perl will silently accept it.

      Enjoy
      ~ Michael

      No, you don't.

      But loop variables should be declared in the loop construct:

      foreach my $item (@list) { }
      or (means the same)
      for my $item (@list) { }
        I printed some of the output because it was not working correctly and it tries to put the hash key value pair in as the directory to copy to and I just want the keys as the directory names to copy to in:

        copy($file, "$to_dir/$dir") or warn "Cannot copy '$file' to '$to_dir/$fi le' $!"