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

Hello everyone , i am trying to do the following split I have a varibale $domain =http://www.ash.com and in $a =http://www.ash.com/ed/example/one.html, i need to split so that i can strip of http;//www.ash.com so i can get the value as "/ed/example/one.html, this is what i am doing below $b = split(/$domain//,$a); but i get an error saying syntax error , can anyone provide me some help thank you

Title edit by tye

Replies are listed 'Best First'.
Re: need some help
by hardburn (Abbot) on May 15, 2003 at 14:14 UTC

    Use URI::URL:

    use strict; use URI::URL; my $a = 'http://www.ash.com/ed/example/one.html'; my $url = URI::URL->new($a); my $path = $url->path();

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: need some help
by suaveant (Parson) on May 15, 2003 at 14:21 UTC
    The syntax error is because you need to escape the backslash like so
    $b = split(/$domain\//,$a);
    But as was suggested, the URI module may be more useful

    Update I was just pointing out the syntax error, since the solution had already been covered.

                    - Ant
                    - Some of my best work - (1 2 3)

      I have to say that $a and $b should not be used! They are package global variables used by sort. Now that I have said that:

      I see a problem... split returns a list not a scalar. Use one of the following types of statements instead:
      @list = split(/$pattern/, $expr);
      $scalar = (split(/$pattern/, $expr))[0];
      ($scalar) = split(/$pattern/, $expr);

      From the docs:

      split

      Splits a string into a list of strings and returns that list. By default, empty leading fields are preserved, and empty trailing ones are deleted.

      In scalar context, returns the number of fields found and splits into the @_ array. Use of split in scalar context is deprecated, however, because it clobbers your subroutine arguments.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      i tried what u showed me , first it didn't work, and second whay would we need to escape the //, i thought perl would understand that , maybe i am wrong , i don't get an error by doing what u said , the output on the screen says "2"