|
Prev: PERL to mean what 'perldoc perl' says is wrong? (was: Re: perlshould be improved and perl6)
Next: Can "perldoc" take input from a pipe?
From: Marek on 15 Apr 2008 11:38 Thank you Sinan and Peter for you prompt answers! Meanwhile I made some progress myself. I am reading in the file name of the unused pix, and then a grep starting with my "pix" folder, and then splitting over '/'. The result is three array elements, and the file name. I am testing, whether the two folders are existing, and if not, create them. Is this a good approach? (My test script follows on the bottom). But I will think over your hints and try to integrate your advices! marek #! /usr/local/bin/perl use strict; use warnings; use Data::Dumper; my $start_dir = "/Users/xyz/Documents/webpages/www.myproject.de"; my $pix_unused_pix = "/Users/xyz/Documents/webpages/www.munich-taxis.de/pix/fotos/ thumbnails/tn_munich17.jpg"; # an example of an unused pix my ($path) = $pix_unused_pix =~ m~/pix/([^"]+?)$~; my @folders = split("/",$path); print "The missing folders are:\n\n"; print Dumper(@folders); print "\n\n"; chdir($start_dir); if (-e $folders[1]) { print "\nYes, your folder /$folders[1] exists!\n\n"; } if (!-e $folders[1]) { print "\nNO! Your folder /$folders[1] does not exists!\n\n"; mkdir $folders[1]; } if (-e $folders[1].'/'.$folders[2]) { print "\nYes, your folder /$folders[1]/$folders[2] exists!\n\n"; } if (!-e $folders[1].'/'.$folders[2]) { print "\nNO! Your folder /$folders[1]/$folders[2] does not exists! \n\n"; mkdir $folders[1] . '/' . $folders[2]; }
From: nolo contendere on 15 Apr 2008 12:31 On Apr 15, 10:18 am, "A. Sinan Unur" <1...(a)llenroc.ude.invalid> wrote: > mkdir $dir unless -d $dir; > > or > > mkdir $dir unless ! -d $dir; > I think here, you meant: mkdir $dir if ! -d $dir;
From: J�rgen Exner on 15 Apr 2008 12:31 Marek <mstep(a)podiuminternational.org> wrote: >Meanwhile I made some progress myself. I am reading in the file name >of the unused pix, and then a grep starting with my "pix" folder, and >then splitting over '/'. The result is three array elements, and the >file name. You may want to look at File::Basename which has functions to split a file path into its components. >I am testing, whether the two folders are existing, and if >not, create them. Is this a good approach? (My test script follows on >the bottom). You may want to look at File::Path which provides mkpath() to create file paths of any depth. >print Dumper(@folders); Why not a simple print @folders; jue
From: Marek on 15 Apr 2008 13:39 Thank you all! Yes I will look into all your suggestions. Meanwhile I am progressing on my own way but I will seriously look into all your suggestions tomorrow. marek *******I am here now! I will make a subroutine in my perl script out of it. If you have any suggestions directly to this script ... #! /usr/local/bin/perl use strict; use warnings; my $start_dir = "/Users/xyz/Documents/webpages/www.myproject.de"; my $pix_out_folder = $start_dir . "/pix_out"; my $pix_unused_pix = "/Users/xyz/Documents/webpages/www.myproject.de/pix/fotos/thumbnails/ tn_munich17.jpg"; my ($path) = $pix_unused_pix =~ m~/pix/([^"]+?)$~; my @folders = split( "/", $path ); if ( !-e $pix_out_folder ) { mkdir $pix_out_folder or die "could not create your folder: $pix_out_folder! $!"; } my $first_sub = shift @folders unless $folders[0] =~ m~\.(jpe?g|gif| png)$~; $first_sub = $pix_out_folder . '/' . $first_sub if $first_sub; if ( $first_sub and !-e $first_sub ) { print "\nNO! Your folder /$folders[1] does not exists!\n\n"; mkdir $first_sub or die "could not create your folder: \"$first_sub \"! $!"; } my $second_sub = shift @folders unless $folders[0] =~ m~\.(jpe?g|gif| png)$~; $second_sub = $first_sub . '/' . $second_sub if $second_sub; if ( $second_sub and !-e $second_sub ) { print "\nNO! Your folder $second_sub does not exists!\n\n"; mkdir $second_sub or die "could not create your folder: \"$second_sub\"! $!"; } my $third_sub = shift @folders unless $folders[0] =~ m~\.(jpe?g|gif| png)$~; $third_sub = $second_sub . '/' . $third_sub if $third_sub; if ( $third_sub and !-e $third_sub ) { print "\nNO! Your folder $third_sub does not exists!\n\n"; mkdir $third_sub or die "could not create your folder: \"$third_sub \"! $!"; }
From: Tad J McClellan on 15 Apr 2008 21:47
John W. Krahn <someone(a)example.com> wrote: > Peter Ludikovsky wrote: >> for(my $i=0;$i<$#filelist;$i++){ > > You have an off-by-one error. You are iterating through the first > element of @filelist through the second-to-last element of @filelist. If you had used the standard Perl idiom foreach my $i ( 0 .. $#filelist ) { instead of a C-style for(;;) loop, you would have had less chance of inserting such a bug... (and whitespace is not a scarce resource, so use some!) -- Tad McClellan email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/" |