Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'A couple of things' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A couple of things
Authored by: pmccann on Jan 09, '02 09:34:36AM
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...

[ Reply to This | # ]
A couple of things
Authored by: pmccann on Jan 09, '02 05:32:47PM

Desperate, lonely old poster cleaning up his own mess....

[[ahem.... the "foreach $file ()" doesn't do what it's supposed to: something like "foreach $file () " should do the trick...]]

Apologies to anyone who might have stared at that sentence for more than a couple of seconds! I'm now guessing that this is what happened to the original post: inside the parentheses was a set of angle brackets --a file glob-- that the geeklog html stripper removed.

I'll submit this one using "Plain Old Text", as any escapes I've tried in html mode have failed miserably. The sentence above should have read:

'something like "foreach $file (<*.jpg>) " should do the trick...'

(The script in my first message is fine...)

Paul, forever battling mini-template languages



[ Reply to This | # ]
A couple of things
Authored by: kevinl on Jan 10, '02 09:54:27PM

hello -
you're right, there was a filename glob in the original script (I'll try to include it again below) -- it is still working for me! My bigger problem was figuring out cron -- I finally settled on having cron run a shell script every half hour that changes to the directory and executes the perl script therein. More than likely crude, but it works for me and gives me different orders every half-hour!

cheers
-kevin

------------------------
#! /usr/bin/perl -w

srand;
foreach $file (<*.jpg>) {
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, $_);}
}



[ Reply to This | # ]
A couple of things
Authored by: pmccann on Jan 11, '02 05:52:21AM

Hi again,

looks like we've slipped off the front page now! Your revised script still has that problem with $pref_ In fact you should be getting a warning when you ask perl to syntax check it:

[pre]% perl -c rename
Name "main::pref_" used only once: possible typo at rename line 6.
rename syntax OK
[/pre]

That is, as per my first post, you're substituting the value of the variable $pref_ --which will be "" (ie empty)-- instead of substituting the value of $pref followed by an underscore. Just curly bracket the pref, like so: ${pref}_

Cheers,
Paul



[ Reply to This | # ]
A couple of things
Authored by: kevinl on Jan 11, '02 12:22:43PM

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!

cheers
-kevin



[ Reply to This | # ]
replying to myself one more time...
Authored by: kevinl on Jan 11, '02 12:35:54PM

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?

I'll be sure to read more carefully in the future...

cheers
-kevin



[ Reply to This | # ]
replying to myself one more time...
Authored by: pmccann on Jan 13, '02 10:29:49AM

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!

Does anyone know whether plain text formatting *supposed* to strip out backslashes (even --yep, I've tried it-- double backslashes)? Any workarounds?

Cheers,
Paul



[ Reply to This | # ]
Version II of this script
Authored by: d1taylor on Jan 12, '02 03:08:27AM
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.


[ Reply to This | # ]
Version II of this script
Authored by: pmccann on Jan 13, '02 10:08:45AM

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...)

As with all the scripts displayed above, the "elsif" condition is redundant (it's no big deal, but you asked!). Might as well just have

else{
#as per above
}

instead of a condition that's already been satisfied. Oh yeah: I wish I could give advice about formatting, but I'm forever stuffing up html submissions so I'll humbly refrain from doing so!

Cheers,
Paul



[ Reply to This | # ]