|
Joined: Feb 2001
Posts: 9
Junior Member
|
Junior Member
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: 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 ]
|
|
|
|
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.
|
|
|
|
Joined: Feb 2001
Posts: 9
Junior Member
|
Junior Member
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...
|
|
|
|
Joined: Feb 2000
Posts: 4,625
Member
|
Member
Joined: Feb 2000
Posts: 4,625 |
Just add this: $Path =~ m!/([^/]+)$!gi;
|
|
|
|
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.
|
|
|
|
Joined: Feb 2001
Posts: 9
Junior Member
|
Junior Member
Joined: Feb 2001
Posts: 9 |
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 ]
|
|
|
|
Joined: Aug 2000
Posts: 335
Member
|
Member
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)
|
|
|
|
Joined: Aug 2000
Posts: 26
Junior Member
|
Junior Member
Joined: Aug 2000
Posts: 26 |
If you use a negative value for the offset, substr will start from the end. From the PERL docs:
|
|
|
|
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.  Dave_L: Habit. *L*
UBB.classic: Love it or hate it, it was mine.
|
|
|
|
Joined: Feb 2001
Posts: 9
Junior Member
|
Junior Member
Joined: Feb 2001
Posts: 9 |
Well... I finally figured out how to use the code you gave me. Thanks, Charles...
|
|
|
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.
|
|
Posts: 449
Joined: February 2008
|
|
Forums63
Topics37,575
Posts293,932
Members13,824
|
Most Online6,139 Sep 21st, 2024
|
|
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
|
|
|
|