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/
pkmichie [ Fr, 21 Januar 2011 16:45 ] [ ID #2053701 ]

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/
jwkrahn [ So, 23 Januar 2011 09:55 ] [ ID #2053703 ]

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/
pkmichie [ Mi, 26 Januar 2011 03:08 ] [ ID #2053882 ]
Perl » gmane.comp.lang.perl.beginners » array like split of string

Vorheriges Thema: string index fucntion
Nächstes Thema: wiki CMS