38 users online (1 members and 37 guests)  


  Results 1 to 2 of 2

Related

  1. Replies: 1
  2. Replies: 16
  3. Replies: 0
  4. Replies: 2
  1. #1
    jwzumwalt's Avatar
    New User

    Status
    Offline
    Join Date
    Jul 2007
    Posts
    6

    Angry Help! Simple text chopping

    I have written a custom directory lister. I am attempting to add bread crumbs (the directory location).
    I can get the envirionment of the calling html page from like this;
    my $request_dir = $ENV{'HTTP_REFERER'};
    It will contain something like this...
    http://www.test.neatinfo.com/html/printenv-pl.html
    I would like to chop this into two parts
    $var1 = "http://www.test.neatinfo.com/"
    For this part, I would really prefer to have it cut at the 3rd "/" so it was not domain dependant.
    $var2 = "./html/"

    For this part, I would prefer for it to cut after the 3rd "/" and before the last "/". This gives just the directory without domain or file name.
    I know this is a piece of cake for perl but I have been struggling with it for two days and just can't seem to get it all working. I found a couple examples on the internet that used substr(), chomp(), and chunk(), but could not figure out exactly what was going on to be able to modify it for my purposes.
    Thanks for the help. FYI you can see my directory lister at work at...
    www.test.neatinfo.com/html/

  2. #2
    xmassey's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Apr 2007
    Posts
    100

    Re: Help! Simple text chopping

    Hello,
    There are a couple of modules available which can perform this task in one simple step. Look into File::Basename and File::Spec. However here is a simple method using standard Perl functions...
    Code:
    $url = "http://www.test.neatinfo.com/html/printenv-pl.html";
    #Split the domain into bits using / as the split symbol...
    @split_url = split(/\//, $url);
    #$split_url[0] is "http:"
    #$split_url[1] is ""
    #$split_url[2] is "www.test.neatinfo.com"
    #$split_url[3] is "html"
    #$split_url[4] is "printenv-pl.html"
    #You can then join parts again to get a more specific url again.
     
    #http://www.test.neatinfo.com would be...
    $var1 = "$split_url[0]//$split_url[2]/";
    print "$var1";
    #/html/ would be...
    $var2 = "/$split_url[3]/";
    print "$var2";
    It depends on what context you are using the url(s) to how you should approach this. For example, the above code would be useful if you have lots of URL's and you want to get the var1 and var2 of all of them. But if you want to perform different tasks on different urls then it would have to be adapted. I have simply rejoined array elements by placing them in order in a string placing /'s back in. However you can remove certain elements of the array and then use join (i.e. $string = join('/', @array). But I definately suggest using File::Basename or File::Spec.

    Chris
    Last edited by xmassey; 11-05-2007 at 11:12 AM.



Tags for this Thread