Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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: http://www.bing.com/default.aspx i want to get the default.aspx off the end of it. i'v tried:

$url = "http://www.bing.com/default.aspx"; @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?

THANKS SO MUCH FOR THE HELP!!!!

Comment on url and arrays
Download Code
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 kennethk (Monsignor) on Jul 01, 2011 at 19:21 UTC
    You've got two mistakes in the posted code. From split
    split /PATTERN/,EXPR,LIMIT
    split /PATTERN/,EXPR
    split /PATTERN/
    split

    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 = "http://www.bing.com/default.aspx"; @parse = split "/", $url; $parse = @parse-1; $FileName = @parse[$parse]; print $FileName;

      TY. yours was the most helpfull

Re: url and arrays
by toolic (Chancellor) on Jul 01, 2011 at 19:31 UTC
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( 'http://www.bing.com/default.aspx' ); say "$_ => $parts{$_}" for grep { defined $parts{$_} } @keys;

    The output is

    scheme => http auth => www.bing.com path => /default.aspx

    Dave

      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.

      --Dave

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 ];

    Or:

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

    Or just:

    my $FileName = ( split "/", $url )[ -1 ];
Re: url and arrays
by sundialsvc4 (Monsignor) 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?
Username:
Password:

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
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2014-08-01 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (25 votes), past polls