Home > Archive > PERL Beginners > December 2006 > What I thought was a simple comparison...
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
What I thought was a simple comparison...
|
|
|
| Any ideas what might be going on with this code segment?
What's happening:
When I initiate the @filesfound array, if there are no *.load files the
grep is working just fine and either populating the array or not as it
meets the requirement.
The first if statement is where I seem to be having the biggest initial
problem but the logic is the same later so I'm just I just don't get
there to find out... Basically, the (@filesfound !~ m/$hostname/)
comparison is never failing...no matter which host I run it from. Any
clues you can throw my way would be greatly appreciated.
Here is the type of file names I'm pulling in as well as creating when
necessary (function earlier in the script that fills ($newname)):
jinx#3.43#12.09#14.83.load
wabbit#13.47#3.01#5.39.load
grumpy#6.51#5.11#8.34.load
Here's the segment of code:
opendir(DIR, $loadDir) || die "Unable to open directory: $!";
my @filesfound = grep(/\.load$/, readdir(DIR));
closedir(DIR);
print "Contents of filesfound array: @filesfound\n"; # Array contains
what it should
if ( @filesfound !~ m/$hostname/) {
open (FILE, ">$newname") || die ("Unable to open file:
$!\n\n");
close(FILE);
}
else {
foreach my $oldname(@filesfound) {
if ($oldname =~ m/$hostname/) {
rename($oldname, $newname) unless
$oldname eq $newname;
}
};
};
Once again, thanks to everyone that's been helping me out.
All the best!
Jake
| |
| Paul Lalli 2006-12-14, 9:58 pm |
| Jake wrote:
> Any ideas what might be going on with this code segment?
>
> What's happening:
>
> When I initiate the @filesfound array, if there are no *.load files the
> grep is working just fine and either populating the array or not as it
> meets the requirement.
>
> The first if statement is where I seem to be having the biggest initial
> problem but the logic is the same later so I'm just I just don't get
> there to find out... Basically, the (@filesfound !~ m/$hostname/)
> comparison is never failing...no matter which host I run it from.
What is it you think such an expression would do? The !~ operator
takes a string on the left and a pattern on the right. A string is a
scalar. Therefore, it is evaluating @filesfound in scalar context. An
array in scalar context returns its size. So you are asking whether or
not the string representing the size of the array does not contain
$hostname. That pretty clearly doesn't make sense.
If you want to say "If none of the members of @filesfound match
$hostname", you have to actually say that:
if (grep /$hostname/, @filesfound == 0) {
Paul Lalli
| |
| nobull67@gmail.com 2006-12-15, 3:58 am |
|
Paul Lalli wrote:
> if (grep /$hostname/, @filesfound == 0) {
You'll more often see this written
if (!grep /$hostname/, @filesfound) {
unless (grep /$hostname/, @filesfound) {
Note the first argument to grep is not an "ordinary" argument where the
expression is evaluated first and the result passed to the grep
function. Instead the first argument is treated as a code block that
grep() will call repeatedly.
Now I know TIMTOWTDI, but one of my top rules in programming is:
"Always use the most natural representation of things unless there is a
reason not to". The natural representation of a block in Perl is {...}
and Perl will allow you to use this so IMNSHO you should...
unless (grep { /$hostname/ } @filesfound) {
(IIRC there's a slight performance hit but it's usually worth it for
the added clarity).
| |
|
| Once again, thank you very much for the help and especially for the
explanations. As I mentioned in my earlier posts I'm new to
programming so sometimes it's difficult to use better practices in my
code. Explanations on why I should do things one way or another are
always greatly appreciated from the knowledge acquisition perspective
if none other.
I finally have the scripts running as I intended thanks to everyone's
help.
Happy Holidays!
Jake
|
|
|
|
|