my @line = $file; for (@line) { ...
This is basically just using $_ instead of $file. Is that really what you want to do? In fact, there are a lot of intermediate variables that could be removed to simplify the code. AFAICT, the following does exactly the same as the code you posted. See how much nicer of a SSCCE this is? If you edit your code down like this, I'll help us immensely, and it may even help you find the bug in the process.
#!/usr/bin/env perl
use warnings;
use strict;
use File::Copy 'move';
my $findme = '/Volumes/photorepos/Perl/SLAperlDropback/';
chdir($findme.'2SLAday') or die "chdir: $!";
foreach my $file (glob "*.*") {
open my $fh, '<', $findme.'dropback.txt' or die "open: $!";
while ( my $row = <$fh> ) {
chomp $row;
if ( $file =~ /$row/ ) {
move( $file, $row ) or die "move: $!";
}
}
}
From this code, I see a couple of things:
- Unless what's in dropback.txt are regular expressions, you probably should say /\Q$row/ (see quotemeta).
- You don't show the contents of dropback.txt. Since $file will probably be plain filenames, if $row is a full pathname, note that if you're checking whether $row contains $file, you've got the regex reversed, and it should be $row =~ /\Q$file/, perhaps with an anchor or other things to prevent it from accidentally matching a string that's just part of a pathname. Or, even better, you already loaded File::Basename but didn't use it - use its fileparse to strip the pathname off, and then do a plain eq on the filenames.
- You use chomp in several places where it isn't needed. If you're worried about excess whitespace, see the corresponding points in the Basic debugging checklist - use either Data::Dumper with $Data::Dumper::Useqq=1;, or use Data::Dump.
If that's not the advice you're looking for, then please better explain the issue with the code, i.e. provide some short sample data and show the values of the variables using the debugging methods I mentioned above at different stages in the loop, explaining how they differ from what you expect.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|