Zentara, Wow! Thank you so much for sharing your knowledge, I learned a lot!
Brian > -----Original Message----- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > Behalf Of zentara > Sent: Monday, February 14, 2005 9:41 AM > To: beginners@perl.org > Subject: Re: Create html pages w/ images and text > > > On Sun, 13 Feb 2005 12:31:48 -0500, [EMAIL PROTECTED] (Brian > Volk) wrote: > > > > I do have ome more question > >if you don't mind.. If I had images w/o text... ( I know > I said that > >wouldn't be the case ) but could I put an else statement > after this bit > >of code so a html page would be created w/ just the image if > there was > >not a corresponding text file? Something like... > > There are a couple of ways you can do it, but think about what is > happening in the loop. We get a list of the .txt files first, and > then loop thru them only. So there is no way, the "loop" can know > about pictures whose basename is not in the loop. > > What you probably want to do, is get a list of both the .txt. and the > .jpg files, and then work your way through them. That is the > way I would > do it anyhow. This has slightly more complexity, because I'm > using some > more advanced methods of finding the difference between 2 arrays. > You could just avoid doing it this way, and make 2 loops one for > txt and one for jpg, and test if the .html file exists yet. > But you want > to try and design things, so you only go thru a loop 1 time. > > #!/usr/bin/perl > use warnings; > use strict; > umask 0022; > > #do a simple glob for .txt files > my @tfiles = <*.txt>; > #strip the extension > (@tfiles) = map{ $_ =~ /(.*).txt$/ } @tfiles; > print "text-> @tfiles\n"; > > #assume photos are in subdir pics/* > #do a simple glob for .jpg files > my @jfiles = <pics/*.jpg>; > (@jfiles) = map{ $_ =~ /pics\/(.*).jpg$/ } @jfiles; > print "jpg-> @jfiles\n"; > > #now find which pics don't have a txt file > my %h; # Initialise the hash using a slice > @[EMAIL PROTECTED] = undef; > @jfiles = grep {not exists $h{$_}} @jfiles; > print "stray jpg-> @jfiles\n"; > > #now push the stray photo onto the end of the @tfiles > push @tfiles,@jfiles; > print "all html-> @tfiles\n"; > > foreach my $file (@tfiles) { > open(HH, "> $file.html") or warn "$!\n"; > my $text = ''; > > #print headline > print HH<<EOH; > <html> > <BODY TEXT="#FFCCCC" BGCOLOR="#000000"> > <center><h1>$file</h1></center> > EOH > ############################### > #print jpg if exists > if( -e "pics/$file.jpg"){ > print HH<<EOI; > <center><IMG SRC="pics/$file.jpg" alt="[$file.jpg]"></center> > EOI > }else{ > print HH<<EOI; > <center><IMG SRC="" alt="[No Image Available]"></center> > EOI > } > ############################ > #print text if exists > if( -e "$file.txt"){ > open(TH, "< $file.txt") or warn "$!\n"; > read( TH, $text, -s TH ); > close TH; > > print HH<<EOT; > <center> > <pre> > $text > </pre> > </center> > EOT > } > ######################### > print HH<<EOHTML; > </body> > </html> > EOHTML > close HH; > > } #end of main loop > __END__ > > > > -- > I'm not really a human, but I play one on earth. > http://zentara.net/japh.html > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > <http://learn.perl.org/> <http://learn.perl.org/first-response> > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>