RE: Help w/map function

2006-10-12 Thread Moon, John
Hi John

Here's your code again, laid out a little more visibly:


use strict;
use warnings;

my @a;

@a = qw/frc.apmt frc_ff.apmt/;
print join(q{,}, map(&subt($_), @a)), "\n\n";

@a = qw/frc.apmt frc_ff.apmt/;
print join(q{,}, map(s/^(.*)\..*/$1/, @a)), "\n\n";

@a = qw/frc.apmt frc_ff.apmt/;
print join(q{,}, map(s/^(.*)\..*/\1/, @a)), "\n\n";

sub subt {
   my ($a) = @_;
   $a =~ s/^(.*)\..*/$1/;
   print "a=$a\n";
   return $a;
}


and the output is:

\1 better written as $1 at E:\Perl\source\x.pl line 13.
a=frc
a=frc_ff
frc,frc_ff

1,1

1,1


In the latter two cases your call to map generates a list consisting of
the
return values of the subtitution operator acting on each list element.
Those
values are the number of substitutions made on the string, so all the
values in
your array are altered in place and your result is a list of 1s as one
substitution was done on each element.

(You will see that adding 'use warnings' caused Perl to chastise you for
using
\1 in the last case instead of the correct $1.)

To write this correctly, use something like


@a = qw/frc.apmt frc_ff.apmt/;
print join(q{,}, map /([^.]*)/, @a), "\n\n";


will do the trick. The return value of the match operator in list
context is the
value of the captured subexpressions, which in this case is all the
non-dot
characters from the beginning of the string. Note that this alternative
doesn't
modify the original array at all.


HTH,

Rob


Thanks you Rob & Paul... jwm

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Help w/map function

2006-10-11 Thread Rob Dixon

Moon, John wrote:
>
> perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,}, map(&subt($_),
> @a)), "\n";
>   sub subt {my ($a) = @_; $a=~s/^(.*)\..*/$1/; print "a=$a\n";
> return $a;}'
> perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,},
> map(s/^(.*)\..*/$1/, @a)), "\n"; '
> perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,},
> map(s/^(.*)\..*/\1/, @a)), "\n"; '
>
> Can someone explain why the last two examples don't product the same
> output as the first?

Hi John

Here's your code again, laid out a little more visibly:


use strict;
use warnings;

my @a;

@a = qw/frc.apmt frc_ff.apmt/;
print join(q{,}, map(&subt($_), @a)), "\n\n";

@a = qw/frc.apmt frc_ff.apmt/;
print join(q{,}, map(s/^(.*)\..*/$1/, @a)), "\n\n";

@a = qw/frc.apmt frc_ff.apmt/;
print join(q{,}, map(s/^(.*)\..*/\1/, @a)), "\n\n";

sub subt {
  my ($a) = @_;
  $a =~ s/^(.*)\..*/$1/;
  print "a=$a\n";
  return $a;
}


and the output is:

\1 better written as $1 at E:\Perl\source\x.pl line 13.
a=frc
a=frc_ff
frc,frc_ff

1,1

1,1


In the latter two cases your call to map generates a list consisting of the
return values of the subtitution operator acting on each list element. Those
values are the number of substitutions made on the string, so all the values in
your array are altered in place and your result is a list of 1s as one
substitution was done on each element.

(You will see that adding 'use warnings' caused Perl to chastise you for using
\1 in the last case instead of the correct $1.)

To write this correctly, use something like


@a = qw/frc.apmt frc_ff.apmt/;
print join(q{,}, map /([^.]*)/, @a), "\n\n";


will do the trick. The return value of the match operator in list context is the
value of the captured subexpressions, which in this case is all the non-dot
characters from the beginning of the string. Note that this alternative doesn't
modify the original array at all.


HTH,

Rob


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Help w/map function

2006-10-11 Thread Paul Johnson
On Wed, Oct 11, 2006 at 07:37:36PM -0400, Moon, John wrote:
> On 10/11/06, Moon, John <[EMAIL PROTECTED]> wrote:
> > perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,}, map(&subt($_),
> > @a)), "\n";
> > sub subt {my ($a) = @_; $a=~s/^(.*)\..*/$1/; print "a=$a\n";
> > return $a;}'
> > perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,},
> > map(s/^(.*)\..*/$1/, @a)), "\n"; '
> > perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,},
> > map(s/^(.*)\..*/\1/, @a)), "\n"; '
> >
> > Can someone explain why the last two examples don't product the same
> > output as the first?
> >
> > Thank you in advance.
> > jwm
> 
> >>Sure. It's the first par of the sub:
> >>
> >>my $a = @_;
> >>
> >>In scalar context, an array returns the number of elements, so your $a
> >>gets 1. Even though you only pass your sub 1 item, @_ is still an
> >>array. It's a 1-element array, but it's an array. What you want is:
> >>
> >>my $a = shift @_;
> >>
> >>Also, for future reference using $a and $b is dangerous and considered
> >>bad form; Perl uses them internally to implement sort.
> >>
> >>HTH
> 
> Thank you for looking but first part of sub is:
> my ($a) = @_;
> 
> and the first test script is returning what I want ... it's the second &
> third I'm asking about

Take a look at the return value of s/// in perldoc perlop.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Help w/map function

2006-10-11 Thread Moon, John
On 10/11/06, Moon, John <[EMAIL PROTECTED]> wrote:
> perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,}, map(&subt($_),
> @a)), "\n";
> sub subt {my ($a) = @_; $a=~s/^(.*)\..*/$1/; print "a=$a\n";
> return $a;}'
> perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,},
> map(s/^(.*)\..*/$1/, @a)), "\n"; '
> perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,},
> map(s/^(.*)\..*/\1/, @a)), "\n"; '
>
> Can someone explain why the last two examples don't product the same
> output as the first?
>
> Thank you in advance.
> jwm

>>Sure. It's the first par of the sub:
>>
>>my $a = @_;
>>
>>In scalar context, an array returns the number of elements, so your $a
>>gets 1. Even though you only pass your sub 1 item, @_ is still an
>>array. It's a 1-element array, but it's an array. What you want is:
>>
>>my $a = shift @_;
>>
>>Also, for future reference using $a and $b is dangerous and considered
>>bad form; Perl uses them internally to implement sort.
>>
>>HTH

Thank you for looking but first part of sub is:
my ($a) = @_;

and the first test script is returning what I want ... it's the second &
third I'm asking about

jwm

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Help w/map function

2006-10-11 Thread Jay Savage

On 10/11/06, Moon, John <[EMAIL PROTECTED]> wrote:

perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,}, map(&subt($_),
@a)), "\n";
sub subt {my ($a) = @_; $a=~s/^(.*)\..*/$1/; print "a=$a\n";
return $a;}'
perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,},
map(s/^(.*)\..*/$1/, @a)), "\n"; '
perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,},
map(s/^(.*)\..*/\1/, @a)), "\n"; '

Can someone explain why the last two examples don't product the same
output as the first?

Thank you in advance.
jwm


Sure. It's the first par of the sub:

   my $a = @_;

In scalar context, an array returns the number of elements, so your $a
gets 1. Even though you only pass your sub 1 item, @_ is still an
array. It's a 1-element array, but it's an array. What you want is:

   my $a = shift @_;

Also, for future reference using $a and $b is dangerous and considered
bad form; Perl uses them internally to implement sort.

HTH

-- jay
--
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.downloadsquad.com  http://www.engatiki.org

values of β will give rise to dom!


Help w/map function

2006-10-11 Thread Moon, John
perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,}, map(&subt($_),
@a)), "\n";
sub subt {my ($a) = @_; $a=~s/^(.*)\..*/$1/; print "a=$a\n";
return $a;}'
perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,},
map(s/^(.*)\..*/$1/, @a)), "\n"; '
perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,},
map(s/^(.*)\..*/\1/, @a)), "\n"; '

Can someone explain why the last two examples don't product the same
output as the first?

Thank you in advance.
jwm

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]