Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

perl/html help

Options
  • 27-05-2005 11:53am
    #1
    Registered Users Posts: 1,598 ✭✭✭


    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 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 Posts: 1,598 ✭✭✭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 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