Code Comments
Programming Forum and web based access to our favorite programming groups.Why does this work....
my $date =3D 'one | two |three |';
my @record =3D map ( whitespace($_), (split /\|/,$_) );
sub whitespace {
my $string =3D shift;
$string =3D~ s/^\s+|\s+$//g;
return $string;
}
but this does not ....
my @record =3D map ( $_=3D~ s/^\s+|\s+$//g,(split /\|/,$_) );
Paul
Post Follow-up to this message> but this does not ....
> my @record =3D map ( $_=3D~ s/^\s+|\s+$//g,(split /\|/,$_) );
Ok i get it. I just need to have it based on a block. Not sure why
both will not work though.
my @record =3D map {$_=3D~s/^\s+|\s+$//g}(split/\|/,$_);
Post Follow-up to this messageOn 4/20/05, Paul Kraus wrote:
> Why does this work....
> my $date =3D 'one | two |three |';
> my @record =3D map ( whitespace($_), (split /\|/,$_) );
No, it won't work - you need to replace the $_ at the end with $date
> sub whitespace {
> my $string =3D shift;
> $string =3D~ s/^\s+|\s+$//g;
> return $string;
> }
>=20
> but this does not ....
> my @record =3D map ( $_=3D~ s/^\s+|\s+$//g,(split /\|/,$_) );
>=20
1. Again, the $_ at the end needs to be $date
2. This doesn't work because the s/// returns the number of
subtitutions made, not the string it changed, so that is what map gets
and passes (a list of numbers). See "perldoc perlop" for details. You
can use the block form of map, as follows:
my @record =3D map {s/^\s+|\s+$//g; $_} split /\|/,$date;
This works because the $_ statement at the end of the block now
constitutes the return value.
3. But there's an even easier way, without having to use map:
my @record =3D split /\s*\|\s*/,$date;
HTH,
--=20
Offer Kaye
Post Follow-up to this messagePaul Kraus wrote:
> Why does this work....
> my $date = 'one | two |three |';
> my @record = map ( whitespace($_), (split /\|/,$_) );
> sub whitespace {
> my $string = shift;
> $string =~ s/^\s+|\s+$//g;
> return $string;
> }
>
> but this does not ....
> my @record = map ( $_=~ s/^\s+|\s+$//g,(split /\|/,$_) );
map() returns the result of the expression "$_=~ s/^\s+|\s+$//g" NOT the val
ue
of $_. If you want it to return the value of $_:
my @record = map { s/^\s+//; s/\s+$//; $_ } split /\|/;
John
--
use Perl;
program
fulfillment
Post Follow-up to this messageOn Wed, 20 Apr 2005 17:04:50 +0300
Offer Kaye <offer.kaye@gmail.com> wrote:
> On 4/20/05, Paul Kraus wrote:
>
> No, it won't work - you need to replace the $_ at the end with $date
>
>
> 1. Again, the $_ at the end needs to be $date
> 2. This doesn't work because the s/// returns the number of
> subtitutions made, not the string it changed, so that is what map gets
> and passes (a list of numbers). See "perldoc perlop" for details. You
> can use the block form of map, as follows:
> my @record = map {s/^\s+|\s+$//g; $_} split /\|/,$date;
> This works because the $_ statement at the end of the block now
> constitutes the return value.
> 3. But there's an even easier way, without having to use map:
> my @record = split /\s*\|\s*/,$date;
------>this seems it cant delete spaces of the last element.
> HTH,
> --
> Offer Kaye
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
--
Whatever you do will be insignificant,but
the important is you do it!
It doesn't matter who you are, it's what
you do that takes you far!
Post Follow-up to this messageOn 4/21/05, FreeFall wrote: >=20 > ------>this seems it cant delete spaces of the last element. >=20 Have you tried it? --=20 Offer Kaye
Post Follow-up to this messageOn Thu, 21 Apr 2005 08:31:27 +0300 Offer Kaye <offer.kaye@gmail.com> wrote: > On 4/21/05, FreeFall wrote: > > Have you tried it? > Sure I did with Paul's example data : $date = 'one | two |three |'; And I tried to change the regx /\s*\|\s*/ to /s*\|?\s*/ and it worked. What do you think? -- Whatever you do will be insignificant,but the important is you do it! It doesn't matter who you are, it's what you do that takes you far!
Post Follow-up to this messageOn 4/21/05, FreeFall wrote:
> Sure I did with Paul's example data :
> $date =3D 'one | two |three |';
> And I tried to change the regx /\s*\|\s*/ to /s*\|?\s*/ and it worked. Wh=
at do you think?
I think that's very strange. Here is my version:
> perl -MData::Dumper -we'use strict; my $date =3D q{one | two |three =
|};my @record =3D split /\s*\|\s*/,$date;print Dumper (\@record);'
$VAR1 =3D [
'one',
'two',
'three'
];
Here is yours:
> perl -MData::Dumper -we'use strict; my $date =3D q{one | two |three =
|};my @record =3D split /\s*\|?\s*/,$date;print Dumper (\@record);'
$VAR1 =3D [
'o',
'n',
'e',
't',
'w',
'o',
't',
'h',
'r',
'e',
'e'
];
As you can see, your version doesn't work correctly.
--=20
Offer Kaye
Post Follow-up to this messageOn Thu, 21 Apr 2005 10:06:24 +0300 Offer Kaye <offer.kaye@gmail.com> wrote: > On 4/21/05, FreeFall wrote: > > I think that's very strange. Here is my version: > $VAR1 = [ > 'one', > 'two', > 'three' > ]; > > Here is yours: > $VAR1 = [ > 'o', > 'n', > 'e', > 't', > 'w', > 'o', > 't', > 'h', > 'r', > 'e', > 'e' > ]; > > As you can see, your version doesn't work correctly. sorry for that. I made a mistake. You are totally right. Thank you,anyway! -- Whatever you do will be insignificant,but the important is you do it! It doesn't matter who you are, it's what you do that takes you far!
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.