For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > WhiteSpace - Map, search replace, split









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author WhiteSpace - Map, search replace, split
Paul Kraus

2005-04-20, 3:56 pm

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
Paul Kraus

2005-04-20, 3:56 pm

> 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/\|/,$_);
Offer Kaye

2005-04-20, 3:56 pm

On 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
John W. Krahn

2005-04-20, 8:56 pm

Paul 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 value
of $_. If you want it to return the value of $_:

my @record = map { s/^\s+//; s/\s+$//; $_ } split /\|/;



John
--
use Perl;
program
fulfillment
FreeFall

2005-04-21, 3:56 am

On 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!
Offer Kaye

2005-04-21, 3:56 am

On 4/21/05, FreeFall wrote:
>=20
> ------>this seems it cant delete spaces of the last element.
>=20


Have you tried it?

--=20
Offer Kaye
FreeFall

2005-04-21, 8:55 am

On 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!
Offer Kaye

2005-04-21, 8:55 am

On 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
FreeFall

2005-04-21, 8:55 am

On 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!
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com