|
|
A couple of things
I think there must be a couple of typo's in this script as posted:
the "foreach $file ()" doesn't do what it's supposed to: something like
foreach $file ()
should do the trick. [[It also avoids renaming the script if (as the description suggests) it's supposed to sit inside the image folder. Actually, the way it was supposed to be invoked might have got around that...]]
Another equivalent option would be to use "foreach $file (@ARGV)", and --assuming you've saved the script as "rename"-- to invoke the thing with "./rename *.jpg"
The second thing is that the "elsif" seems to have an error in it: "$pref_" is going to be the value of, well... ${pref_} (ie the scalar variable with the name pref_). I think that should be changed to ${pref}_ in the second slot of the substitution operator.
In summary, (trying to keep close to what was posted), something like the following should work as advertised:
#!/usr/bin/perl -w
srand;
foreach $file (@ARGV) {
if ($file !~ /^[0-9]+_/ ) {
$pref=int rand(100);
$new=$pref."_".$file;
rename ($file, $new);}
elsif ($file =~ /^[0-9]+_/) {
$pref=int rand(100);
$_=$file;
s/[0-9]+_/${pref}_/;
rename ($file, $_);}
}
Cheers,
Paul (about to regret a silly attempt to post "html formatted"!) Nope, preview to the rescue...
A couple of things
Desperate, lonely old poster cleaning up his own mess....
A couple of things
hello -
A couple of things
Hi again,
A couple of things
ah ha! I think I see the problem -- in the original script, I escaped the underscore after $pref -- so it was substituting $pref and then an underscore, not the variable $pref_ TMTOWTDI!
replying to myself one more time...
my bad -- I guess the escape character didn't come across in the posting of the script! There should be a backslash between pref and the underscore in the substitution, but it just won't display for some reason?
replying to myself one more time...
Wow, this thread could serve as a very effective warning for anyone trying to submit scripts! Thanks for the explanation of what was going wrong: it seemed strange to me that you weren't getting the warnings that were slapping my face every time I would cut and paste your script. As is usually the case in such situations, there's some noise in the channel!
Version II of this script
I have taken Kevin's script and made a bunch of tweaks and changes to have it be a bit more general purpose. I've also added some comments and unwrapped the code to be a bit more readable:
#!/usr/bin/perl -w
# Written by KevinL
# with minor tweaks by Dave Taylor <taylor@intuitive.com>
#
# Note: this is best run by cron every day, so that your images are
# constantly shuffling around. To do that, you'll want to make sure
# that this program switches to the correct directory, which is most
# easily done by including the following line (with the directory
# specification changed to your own Screen Saver Images directory,
# of course!):
# chdir("/Users/kevin/Pictures/Desktop Pictures") || die "I can't chdir";
srand; # initialize random number package
foreach $file (<*.[jJ][pP][gG]>) {
if ($file !~ /^[0-9]+_/ ) {
$pref=int rand(100);
$new=$pref."_".$file;
rename ($file, $new);
}
elsif ($file =~ /^[0-9]+_/) {
$pref=int rand(100);
$_=$file;
s/[0-9]+_/$pref_/;
rename ($file, $_);
}
}
Comments??
Btw, how do you avoid having code listings in a <PRE> double-spaced? I have post mode = HTML Formatted, but it's still adding <br> after each line. Blech.
Version II of this script
Given Kevin's explanation of the problem with backslashing the underscore in the substitution operator I'll assume that you've done that (and had it promptly stripped by the formatter...) |
SearchFrom our Sponsor...Latest Mountain Lion HintsWhat's New:HintsNo new hintsComments last 2 daysNo new commentsLinks last 2 weeksNo recent new linksWhat's New in the Forums?
Hints by TopicNews from Macworld
From Our Sponsors |
|
Copyright © 2014 IDG Consumer & SMB (Privacy Policy) Contact Us All trademarks and copyrights on this page are owned by their respective owners. |
Visit other IDG sites: |
|
|
|
Created this page in 0.08 seconds |
|