Quoth "John W. Krahn" <>:
> Terry wrote:
> > I am a few hours into my life after Perl.
> >
> > I need to rename approximately 5000 .jpg files, which I think I have
> > figured out thanks to "Learning Perl" 3rd. Ed. (pg. 176).
> >
> > Unlike that example, I would like to compare/match the file names to a
> > table/field in a csv file and once matched rename the file to the
> > second table/field in the csv file so that ABCDEFG.jpg becomes
> > 1111111.jpg and ABCDEFH.jpg becomes 1111112.jpg and so on.
> >
> > I've been searching my perldoc, comp.lang.perl.misc and "Learning Perl"
> > most of the day and haven't found anything that looks like what I want
> > to do, but I'm chalking that up to not knowing where to look.
> >
> > Any suggestions of howto: or where to look would be greatly
> > appreciated.
>
> UNTESTED:
And wrong.
> open my $csv, '<', 'csv_file' or die "Cannot open 'csv_file' $!";
>
> while ( <$csv> ) {
> chomp;
> my ( $from, $to ) = split /,/;
CSV is actually quite complicated for such a simple format, and this is
not the right way to parse it. Use a module, such as Text::CSV_XS.
> if ( -e $from and not -e $to ) {
> rename $from, $to or warn "Cannot rename '$from' $!";
> }
> }
>
> close $csv;
If you're not going to check the return value, there's no point doing an
explicit close.
Ben
--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
'Alcestis') [ flame, and falls out of sight. ]