Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

url and arrays

by perlaintdead (Scribe)
on Jul 01, 2011 at 19:10 UTC ( #912402=perlquestion: print w/replies, xml ) Need Help??
perlaintdead has asked for the wisdom of the Perl Monks concerning the following question:

hello. im trying to get the file name off the end of a url Example: i want to get the default.aspx off the end of it. i'v tried:

$url = ""; @parse = split($url, "/"); $parse = @parse; $FileName = @parse[$parse]; print $FileName;

but it didn't work. is my syntax off or am i just taking the wrong approach?


Replies are listed 'Best First'.
Re: url and arrays
by kennethk (Abbot) on Jul 01, 2011 at 19:21 UTC
    You've got two mistakes in the posted code. From split
    split /PATTERN/,EXPR
    split /PATTERN/

    So you mean @parse = split("/", $url); not @parse = split($url, "/");. As well, array indexing starts from 0, not 1, so @parse[$parse]; is out-of-bounds -- see Variable names. You can fix this either with $parse = $#parse; or $parse = @parse - 1;.

    Corrected version:

    $url = ""; @parse = split "/", $url; $parse = @parse-1; $FileName = @parse[$parse]; print $FileName;

      TY. yours was the most helpfull

Re: url and arrays
by davido (Archbishop) on Jul 01, 2011 at 19:52 UTC

    URL's can be tricky. Maybe your case is fairly trivial, but why not use a tool known to work? URI::Split

    use Modern::Perl; use URI::Split qw/uri_split/; my %parts; @parts{ my @keys = qw/ scheme auth path query frag / } = uri_split( '' ); say "$_ => $parts{$_}" for grep { defined $parts{$_} } @keys;

    The output is

    scheme => http auth => path => /default.aspx


      URL's can be tricky. Maybe your case is fairly trivial, but why not use a tool known to work? URI::Split

      Not only is it known to work, but it is intended to continue working, for the OP's specific intended purpose. When found not to work, it shall be fixed. It's the right solution today and tomorrow. That's the textbook reason for reusing code like this.


Re: url and arrays
by Corion (Pope) on Jul 01, 2011 at 19:21 UTC

    What value does $parse contain?

    What is the index of the last element of @parse?

    Most likely, you want $#parse and not 0+@parse, but I'd use a regular expression instead:

    $url =~ m!/([^/]+)$! or die "Couldn't find last element of '$url'"; my $FileName = $1; print $FileName;
Re: url and arrays
by jwkrahn (Monsignor) on Jul 01, 2011 at 20:36 UTC
    @parse = split($url, "/"); $parse = @parse; $FileName = @parse[$parse];

    That should be:

    my @parse = split "/", $url; my $parse = -1; my $FileName = $parse[ $parse ];


    my @parse = split "/", $url; my $FileName = $parse[ -1 ];

    Or just:

    my $FileName = ( split "/", $url )[ -1 ];
Re: url and arrays
by toolic (Bishop) on Jul 01, 2011 at 19:31 UTC
Re: url and arrays
by sundialsvc4 (Abbot) on Jul 02, 2011 at 18:59 UTC

    Usually, I like to use one of the Regexp::Common::URI regular expressions.   (There are also various other CPAN packages specifically written for dealing with URI's.)   Basically, any time that I can shove a task off of my own plate, on to someone else’s, knowing also that this person has also written an entire test-suite and is delivering code that passes all those tests ... I’m gonna do that.

    Now, if you do find yourself with the need to “roll your own” regexes, you’ll need to pay particular attention to the concept of greedy vs. non-greedy evaluation.   If, for instance, you write a regex that uses something to grab “all the characters from a slash to the end of the string,” I might be wrong because my regex would be considered greedy, and it would match the longest string that it could ... thereby shoveling up additional slashes because the largest result would be obtained from matching the leftmost slash.

    It is these subtleties, and others, that prompt me to seek out pre-tested work that comes neatly packaged in a convenient CPAN spray-bottle.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://912402]
Approved by kennethk
Front-paged by luis.roca
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-04-19 10:41 GMT
Find Nodes?
    Voting Booth?