Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

perl/html help

  • 27-05-2005 11:53AM
    #1
    Registered Users, Registered Users 2 Posts: 1,602 ✭✭✭


    could anyone help me finish this program.

    i need to write a program which reads the file structure of a directory and prints out a list of all the html files in order( i have done this part) but then i need to be able to use that list so any one of the files can be selected through a html web page and this is the bit that is giving me trouble.

    sorry if i havent explained it well.

    heres the code:


    #!/usr/bin/perl -w
    use IO::Dir;
    use CGI;

    my @fileList;

    $dir = IO::Dir->new(".");
    if (defined $dir)
    {
    while (defined($_ = $dir->read))
    {
    if (/\.html$/)
    {
    push @fileList, $_;
    }
    }
    }

    @fileList = sort {uc($a) cmp uc($b)} @fileList;

    my $cgi = new CGI;
    print $cgi->header();
    print $cgi->start_html;
    print "<form name="List Dir" method="post" action="">";
    print "<select name="select">";




    foreach my $file (@fileList)
    {
    print <option value=$file>$file</option>;

    }
    print "</select>";
    print "</form>";
    print $cgi->end_html;

    }


Comments

  • Registered Users, Registered Users 2 Posts: 90 ✭✭CasimiR


    so you want to pass on selected files ?
    should be something like that then :)
    print $co->header,
    $co->start_html(
     -title=>'Page Title ',
     -BGCOLOR=>'#FFFFFF',
     ),
    $co->start_form(
     -method=>'POST',
     -action=>"location of the script or page you are posting to",
    ),
    
    
    $co->popup_menu( -name=>'Files',
                     -default=>'-',
                     -multiple => 'true',
                     -size=>15,
                     -width=>20,
                     -values=>[@fileList]),
    
    $co->submit( -name=>'NEXT >>',
                  ),
    
    $co->end_html;
    END{}
    __END__
    
    


  • Registered Users, Registered Users 2 Posts: 1,602 ✭✭✭joe316


    what i thought i could do was read through the array of files into a selection box where one can be selected from the list.


  • Registered Users, Registered Users 2 Posts: 1,268 ✭✭✭hostyle


    opendir(TEMP, $basedir);
    @temp_dir = grep(!/^\./, readdir (TEMP));
    close (TEMP);
    
    print "<select>";
    foreach (sort @temp_dir) {
        if (-f "$basedir/$_") {
    	print "<option value=\"$_\">$_</option>";
        }
    }
    print "</select>";
    

    Change the grep bit to whatever you need.


  • Closed Accounts Posts: 304 ✭✭Zaltais


    I'm assuming what you've entered is litteraly the code you've entered, which is giving you some errors.

    See below for a commented version of your code with some pointers.
    #!/usr/bin/perl -w
    
    # It's considered best practice to 'use strict'. Ask me / google if you want to know why.
    # It does make life a touch harder when you're learning though (but makes you a better programmer!)
    
    use strict;
    
    use IO::Dir;
    use CGI;
    
    my @fileList;
    
    my $dir = IO::Dir->new(".");  # See 'use strict' above
    if (defined $dir)
    {
    while (defined($_ = $dir->read))
    {
    if (/\.html$/)
    {
    push @fileList, $_;
    }
    }
    }
    
    @fileList = sort {uc($a) cmp uc($b)} @fileList;
    
    my $cgi = new CGI;
    print $cgi->header();
    print $cgi->start_html;
    
    # Ok you've major problems on the two lines below.
    print "<form name="List Dir" method="post" action="">";
    print "<select name="select">";
    
    # Figured out what's wrong with them? If not take a look at your double quotes.
    # Now do you see?
    # Ask if you still can't see what's wrong.
    
    
    foreach my $file (@fileList)
    {
    # Major problems in the line below again.
    print <option value=$file>$file</option>;
    
    # Think about the lines above with the double quotes again....
    
    }
    print "</select>";
    print "</form>";
    print $cgi->end_html;
    
    } # What's this for?
    
    


    As I said, ASK if you've any questions about any of the above. PM me if you want to keep it 'off-list' as it were.


Advertisement