Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

My code is as follows:-
@logfiles=("access.html","access_log","log.txt","log.html"); @logfolders=("accesslogdir","logs"); @cgifolders=("cgi","cgi-bin","scripts"); @cgifiles=("script.pl","test.pl","tools.pl"); $baseurl="http://www.testurl.com"; foreach $folder(@logfolders) { foreach $file(@logfiles) { my($url)=join '',$baseurl,$folder,"/",$file; print "Compiled URL - $url\n"; }
I actually want to be able to do all variations of the above including all folders and all files in all the four arrays. but i also want to hit urls that are just off the main bsae url i.e. instead of running through as above I also want to be able to access urls such as http://www.testurl.com/test.pl instead of http://www.testurl.com/cgi-bin/test.pl this gets messed up as my way of doing it will always add the $folder variable into the compiled url via the join statement !!!. I know there will be an easy way of doing this but my brain has switched off !!!! Hard to explain, hopefully someone will get my gist :)

Replies are listed 'Best First'.
Re: How to list all possible variations ?
by BrowserUk (Patriarch) on Jan 15, 2003 at 21:33 UTC

    Something like this?

    #! perl -slw use strict; my @logfiles=("access.html","access_log","log.txt","log.html"); my @logfolders=("accesslogdir","logs"); my @cgifolders=("cgi","cgi-bin","scripts"); my @cgifiles=("script.pl","test.pl","tools.pl"); my $baseurl="http://www.testurl.com"; for my $folder ( @logfolders, @cgifolders, '' ) { for my $file (@logfiles) { (my $combURL = "$baseurl/$folder/$file") =~ tr[/][]s; print $combURL; } } __END__ C:\test>227227 http:/www.testurl.com/accesslogdir/access.html http:/www.testurl.com/accesslogdir/access_log http:/www.testurl.com/accesslogdir/log.txt http:/www.testurl.com/accesslogdir/log.html http:/www.testurl.com/logs/access.html http:/www.testurl.com/logs/access_log http:/www.testurl.com/logs/log.txt http:/www.testurl.com/logs/log.html http:/www.testurl.com/cgi/access.html http:/www.testurl.com/cgi/access_log http:/www.testurl.com/cgi/log.txt http:/www.testurl.com/cgi/log.html http:/www.testurl.com/cgi-bin/access.html http:/www.testurl.com/cgi-bin/access_log http:/www.testurl.com/cgi-bin/log.txt http:/www.testurl.com/cgi-bin/log.html http:/www.testurl.com/scripts/access.html http:/www.testurl.com/scripts/access_log http:/www.testurl.com/scripts/log.txt http:/www.testurl.com/scripts/log.html http:/www.testurl.com/access.html http:/www.testurl.com/access_log http:/www.testurl.com/log.txt http:/www.testurl.com/log.html C:\test>


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

      works excellent browseruk except that it is stripping one of the fwd slashes from the baseurl i.e. it is turning http://www.testurl.com into http:/www.testurl.com

        I completely missed that. Substitute these two lines.

        (my $combURL = "/$folder/$file") =~ tr[/][]s; print "$baseurl$combURL";

        Examine what is said, not who speaks.

        The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Re: How to list all possible variations ?
by Enlil (Parson) on Jan 15, 2003 at 21:37 UTC
    1. use strict;

    2. You need to close your first foreach statement.

    But what I think you want can be accomplished like so:

    use strict; use warnings; my @logfiles=("access.html","access_log","log.txt","log.html"); my @logfolders=("accesslogdir","logs"); my @cgifolders=("cgi","cgi-bin","scripts"); my @cgifiles=("script.pl","test.pl","tools.pl"); my $baseurl="http://www.testurl.com/"; foreach my $folder (@logfolders) { my @url; foreach my $file(@logfiles) { $url[0]=join '',$baseurl,$folder,"/",$file; $url[1]=join '',$baseurl ,$file; print "Compiled URLs - $url[0], $url[1]\n"; } }
    I am assuming that you would be creating a second set of loops for the cgi stuff.

    -enlil

Re: How to list all possible variations ?
by dragonchild (Archbishop) on Jan 15, 2003 at 21:21 UTC
    foreach my $folder (@logfolders, '') { # Do rest of stuff here }
    As a very important aside, use strict! It will save you a ton of headaches, probably in the near future.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      This doesnt seem to work, I also want to so all the arrays in one subroutine and this will only do the @logfolders
        And I'm not write your script for you. Either:
        • you can figure out how to extend a suggested fix to be what you need it to be
        • you need to learn do so
        (Unless you're willing to pay me my going rate, of course.)

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.