array like split of string
I have this regex expression in a script that appears to do an array
like split of a string but I cannot figure out how it does so. Any
help appreciated
$fname = ($0 =~ m[(.*/)?([^/]+)$])[1] ;
print "7 $errlog\n";
$fpath = ($0 =~ m[(.*/)?([^/]+)$])[0] ;
print "8 $errlog\n";
The array elements 0 and 1 above extract the path to the executable
and the executable filename but there is no array definition anywhere
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: array like split of string
Peter K. Michie wrote:
> I have this regex expression in a script that appears to do an array
> like split of a string but I cannot figure out how it does so. Any
> help appreciated
>
> $fname = ($0 =~ m[(.*/)?([^/]+)$])[1] ;
> print "7 $errlog\n";
>
> $fpath = ($0 =~ m[(.*/)?([^/]+)$])[0] ;
> print "8 $errlog\n";
>
>
> The array elements 0 and 1 above extract the path to the executable
> and the executable filename but there is no array definition anywhere
A regular expression with capturing parentheses will return a list of
those captures in list context. The particular item from the list is
extracted via a list slice with one index.
You could combine those two captures and you wouldn't need the list slice:
( $fpath, $fname ) = $0 =~ m[(.*/)?([^/]+)$]
Or, what you should do is use a module designed for this specific task:
use File::Basename;
my $fname = basename( $0 );
my $fpath = dirname( $0 );
Or perhaps use the File::Spec module.
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: array like split of string
On Jan 23, 2:55=A0am, jwkr... [at] shaw.ca ("John W. Krahn") wrote:
> Peter K. Michie wrote:
> > I have this regex expression in a script that appears to do an array
> > like split of a string but I cannot figure out how it does so. Any
> > help appreciated
>
> > $fname =3D ($0 =3D~ m[(.*/)?([^/]+)$])[1] ;
> > print "7 $errlog\n";
>
> > $fpath =3D ($0 =3D~ m[(.*/)?([^/]+)$])[0] ;
> > print "8 $errlog\n";
>
> > The array elements 0 and 1 above extract the path to the executable
> > and the executable filename but there is no array definition anywhere
>
> A regular expression with capturing parentheses will return a list of
> those captures in list context. =A0The particular item from the list is
> extracted via a list slice with one index.
>
> You could combine those two captures and you wouldn't need the list slice=
:
>
> ( $fpath, $fname ) =3D $0 =3D~ m[(.*/)?([^/]+)$]
>
> Or, what you should do is use a module designed for this specific task:
>
> use File::Basename;
>
> my $fname =3D basename( $0 );
> my $fpath =3D dirname( $0 );
>
> Or perhaps use the File::Spec module.
>
> John
> --
> Any intelligent fool can make things bigger and
> more complex... It takes a touch of genius -
> and a lot of courage to move in the opposite
> direction. =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -- Albert Einstein
Thank you for the reply.
Much appreciated
Peter
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/