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,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;
| [reply] [d/l] [select] |
|
|
| [reply] |
Re: url and arrays
by davido (Cardinal) 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
| [reply] [d/l] [select] |
|
|
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.
| [reply] |
Re: url and arrays
by Corion (Patriarch) 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;
| [reply] [d/l] [select] |
Re: url and arrays
by jwkrahn (Abbot) 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 ];
| [reply] [d/l] [select] |
Re: url and arrays
by toolic (Bishop) on Jul 01, 2011 at 19:31 UTC
|
| [reply] |
Re: url and arrays
by locked_user 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.
| |