Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have a path such as:

where I want to just get the index.html from.

So if I split it up I can just print this part "index.html"


I think I need alot of help with this part??

$mypage = "my $page = split /\//, $mypage;
print $page; #This should just print "index.html" here but I dont know how to get it??
 
Code:
my @dirs = split /\//, $mypage;
my $page = $dirs[$#dirs];
 
Thanks.

Never seen the "$#dirs" part. What is that doingt??
And how can I fetch each part to the "

I know the "print $page;" gives me "index.html" but what if I want to print "mypage" part of the url? Would I do a print "$page[1]" which didnt work. Please explain if you have the time?
 
$#dirs is the index to the last entry in the array @dirs.

The split creates one array element for each / in the string, so after the command

my @dirs = split /\//, $mypage;

@dirs looks like this:
$dirs[0] = http:
$dirs[1] = ""
$dirs[2] = mypage
$dirs[3] = directory
$dirs[4] = index.html

$#dirs = 4, since it's the last array index, and the // after the http: creates an empty element in the array, which is why you didn't get anything looking at $dirs[1].

Richard
 
If you don't need the rest of the path, just the page, then you can eliminate @dirs and just do
Code:
my $page = (split /\//, $mypage)[-1];
The '-1' picks off the last value of the split.
jaa
 
Or,
Code:
$path = '/path/to/the/file/of/interest/tt-junk';
$file = pop @{[split /\//, $path]};
print "File: $file\n";

T(always)MTOWDI ;-) 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top