Hi! Has anyone got five minutes to spare to be able to tell me how to get a list of files in the current directory with <option></option> around each file? So far I've written; print "<select name=file><option>"; print `ls\n`; print "</option></select>"; But naturally, I need the <option> bit around each filename! Please help! Dug Stokes
--- Dug Stokes
Hi!
Has anyone got five minutes to spare to be able to tell me how to get a list of files in the current directory with <option></option> around each file?
So far I've written;
print "<select name=file><option>";
print `ls\n`;
print "</option></select>";
But naturally, I need the <option> bit around each filename!
Please help!
Dug Stokes
Hey Dug, Try This................... -------------------------------------- #!/usr/bin/perl -w opendir(DIRHANDLE,"/home/abc123"); @filenames = readdir(DIRHANDLE); open(OUT, ">~/output.txt") || die "eek!!" foreach (@filenames) { #process each file name while (<OUT>) { print "<option>$_</option>\n"; } } -------------------------------------------- :) -- Thomas Adam ===== Thomas Adam "The Linux Weekend Mechanic" -- www.linuxgazette.com __________________________________________________ Yahoo! Plus For a better Internet experience http://www.yahoo.co.uk/btoffer
On Tuesday 06 May 2003 9:25 am, Dug Stokes wrote:
Hi!
Has anyone got five minutes to spare to be able to tell me how to get a list of files in the current directory with <option></option> around each file?
So far I've written;
print "<select name=file><option>";
print `ls\n`;
print "</option></select>";
But naturally, I need the <option> bit around each filename!
Please help!
Dug Stokes
My Perl is a little rusty, to coin a phrase, but what about: #!/usr/bin/perl -w print "<select name=file>\n"; while (<>) { print "<option>$_</option>\n"; } print "</select>\n"; and invoke it with ls -bA | ./t.pl (works for me) cheers timp
On Tuesday 06 May 2003 10:25 am, Dug Stokes wrote:
Hi!
Has anyone got five minutes to spare to be able to tell me how to get a list of files in the current directory with <option></option> around each file?
So far I've written;
print "<select name=file><option>";
print `ls\n`;
print "</option></select>";
But naturally, I need the <option> bit around each filename!
Please help!
Dug Stokes
You could do something like @files=`ls`; # no '\n' needed print "<select name=file>\n"; # note I've moved the <option> foreach(@files) { print "<option>$_</option>\n"; } print "</select>"; but for portability it's better to use perl functions - that way, for example the same perl code would work on a windows box. Try something like: print "<select name=file>\n"; # note I've moved the <option> print "<option>$_</option>\n" while (<*>); print "</select>"; Gary -- Gary Stainburn This email does not contain private or confidential material as it may be snooped on by interested government parties for unknown and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
You could do something like
@files=`ls`; # no '\n' needed print "<select name=file>\n"; # note I've moved the <option> foreach(@files) { print "<option>$_</option>\n"; } print "</select>";
but for portability it's better to use perl functions - that way, for example the same perl code would work on a windows box. Try something like:
print "<select name=file>\n"; # note I've moved the <option> print "<option>$_</option>\n" while (<*>); print "</select>";
Yes, Gary :) A "globbing" pattern such as "<*>" is how one *should* open files in perl, anyway :) -- Thomas Adam ===== Thomas Adam "The Linux Weekend Mechanic" -- www.linuxgazette.com __________________________________________________ Yahoo! Plus For a better Internet experience http://www.yahoo.co.uk/btoffer
Dug Stokes
print "<select name=file><option>"; print `ls\n`; print "</option></select>"; But naturally, I need the <option> bit around each filename!
Quick and dirty, based on your code: print '<select name="file"><option>' . join("</option><option>",map({chomp; $_} `/bin/ls`)) . '</option></select>'; map(fn array) is an often-overlooked function to apply a function to every element of an array, here used to trim the trailing end of lines. join(glue,array) sticks an array together with the glue between them. The dots are string concatenation. -- MJR http://mjr.towers.org.uk/ IM: slef@jabber.at This is my home web site. This for Jabber Messaging. How's my writing? Let me know via any of my contact details.
participants (5)
-
Dug Stokes
-
Gary Stainburn
-
MJ Ray
-
Thomas Adam
-
Tim Pizey