Previous Thread
Next Thread
Print Thread
Rate Thread
#102197 02/12/2001 6:01 PM
Joined: Feb 2001
Posts: 9
PSX
Offline
Junior Member
Junior Member
Offline
Joined: Feb 2001
Posts: 9
Well here's my problem. I have a variable that is equal to an absolute path. Then I need to take a substring of it, starting from the end until it reaches a /. For example, let's say I have a variable:

Code
code:

Basically, what I want to pull out is the "Public" part. But I don't know what the last part is in the program I have written... Any ideas?

Thanks.

[ February 12, 2001: Message edited by: PSX ]

Sponsored Links
#102198 02/12/2001 7:22 PM
Joined: Jan 2000
Posts: 5,073
Admin Emeritus
Admin Emeritus
Joined: Jan 2000
Posts: 5,073
Solution:
$Path =~ m!/([^/]+)$!gi;
my $value = $1;

Explained:
m!
Start a match, delimiter is !.

/
A slash

(
Start a group

[^/]
A single character that is not a forward slash

One or more of the previous single character
+


)
End group

$
End of string

!gi;
End regex. Make pattern searching global and case insensitive.

The value inside the first group is automagically stuck into $1 by Perl.


UBB.classic: Love it or hate it, it was mine.
#102199 02/12/2001 7:35 PM
Joined: Feb 2001
Posts: 9
PSX
Offline
Junior Member
Junior Member
Offline
Joined: Feb 2001
Posts: 9
I'm not exactly understanding this. Could you possibly give me an actual example or can this be used in conjuction with substr? I'm a little confused...

#102200 02/12/2001 8:58 PM
Joined: Feb 2000
Posts: 4,625
Member
Member
Offline
Joined: Feb 2000
Posts: 4,625
Just add this:
$Path =~ m!/([^/]+)$!gi;

#102201 02/12/2001 11:53 PM
Joined: Jan 2000
Posts: 5,073
Admin Emeritus
Admin Emeritus
Joined: Jan 2000
Posts: 5,073
I gave you the code required above - BOTH lines, not just the first that MM quoted.

You can not do this just by using substr(), as it requires a static start position.


UBB.classic: Love it or hate it, it was mine.
Sponsored Links
#102202 02/13/2001 10:33 AM
Joined: Feb 2001
Posts: 9
PSX
Offline
Junior Member
Junior Member
Offline
Joined: Feb 2001
Posts: 9
Quote
quote:
Well the position I want to start at is at the end of the path. Maybe I should re-explain my problem. I have a variable equal to a path. Like "/data1/va/UBB/Public" The last part of the path "Public" could be anything. So starting from the end I need to pull out anything until it hits a slash (/). So in that case I want it to pull out "Public". If the path was "/data1/va/UBB/Pub", then it would pull out "Pub". I'm sorry if this is the same thing I said, but what you are giving me, isn't exactly making the most sense to me... I think I understand what you have done, but not exactly how to apply it to what I am doing.

[ February 13, 2001: Message edited by: PSX ]

#102203 02/13/2001 1:45 PM
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
PSX - the method Charles gave you does what you want.

Charles - Why are you using the 'g' and 'i' modifiers here? (they seem unnecessary)

#102204 02/13/2001 2:56 PM
Joined: Aug 2000
Posts: 26
Junior Member
Junior Member
Offline
Joined: Aug 2000
Posts: 26
If you use a negative value for the offset, substr will start from the end.

From the PERL docs:
Quote
quote:

#102205 02/13/2001 4:01 PM
Joined: Jan 2000
Posts: 5,073
Admin Emeritus
Admin Emeritus
Joined: Jan 2000
Posts: 5,073
Yes, and if you don't know how long your path is, you CAN'T use substr. smile

Dave_L: Habit. *L*


UBB.classic: Love it or hate it, it was mine.
#102206 02/13/2001 4:15 PM
Joined: Feb 2001
Posts: 9
PSX
Offline
Junior Member
Junior Member
Offline
Joined: Feb 2001
Posts: 9
Well... I finally figured out how to use the code you gave me. Thanks, Charles...

Sponsored Links

Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
JAISP
JAISP
PA
Posts: 449
Joined: February 2008
Forum Statistics
Forums63
Topics37,575
Posts293,932
Members13,824
Most Online6,139
Sep 21st, 2024
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,835
Greg Hard 4,625
Top Posters(30 Days)
Gizmo 2
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2025 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.0.1
(Snapshot build 20240918)