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!

ksh equivalent 2

Status
Not open for further replies.

cryptoadm

MIS
Nov 6, 2008
71
US
Out of curiosity what are the Perl equivalents to ksh:

$ var=abc/123/def
$ echo ${var##*/}
def
$ echo ${var#*/}
123/def
$ echo ${var%%/*}
abc
$ echo ${var%/*}
abc/123
 
As var as I know (and I'm no Perl wizard) you have to do it in multiple steps in Perl... I'd also be keen to know if there is such a shortcut which leaves the original variable unmodified? I have been doing something ugly like this:

Code:
$var1 = $var;   # prevent original 
$var1 =~ s/.*\///;
print "$var1\n";

Annihilannic.
 
Hi

Code:
[u]  DB<1> [/u]$var='abc/123/def';

[u]  DB<2> [/u]print $var=~m,.*/(.*),;
def

[u]  DB<3> [/u]print $var=~m,.*?/(.*),;
123/def

[u]  DB<4> [/u]print $var=~m,(.*?)/,;
abc

[u]  DB<5> [/u]print $var=~m,(.*)/,;
abc/123

Feherke.
 
I was using index, rindex, and substr to get them but it took multiple variables like this:

$pos = rindex("$_", "#");
$pos1 = rindex("$_", "/");
$path = substr("$_", 0, $pos1);
$flen = length($_);
$flen1 = ($flen-$pos1);
$filename = substr("$_", $pos1+1, $flen1);
$year = substr("$_", $pos+1, 4);
$year1 = substr("$year", 2, 2);
$month = substr("$_", $pos+5, 2);
 
Hi

cryptoadm, I only answered your question. I do not suggest to use my answer. (-:

Supposing your question is related to thread205-1538095, where your problem was the execution speed, better go with your function based solution. Regular expressions are nice and short, but usually they are not speed champions.

Feherke.
 
While discussing ||= on this thread I found this handy tip on man perlop:

Code:
Unlike in C, the scalar assignment operator produces a valid lvalue.
Modifying an assignment is equivalent to doing the assignment and then
modifying the variable that was assigned to.  This is useful for
modifying a copy of something, like this:

    ($tmp = $global) =~ tr [A-Z] [a-z];

Which means my code above can be shortened to:

Code:
( $var1 = $var ) =~ s/.*\///;
print "$var1\n";

Much more ksh-like.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top