Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

WhiteSpace - Map, search replace, split
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

Report this thread to moderator Post Follow-up to this message
Old Post
Paul Kraus
04-20-05 08:56 PM


Re: WhiteSpace - Map, search replace, split
> 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/\|/,$_);

Report this thread to moderator Post Follow-up to this message
Old Post
Paul Kraus
04-20-05 08:56 PM


Re: WhiteSpace - Map, search replace, split
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

Report this thread to moderator Post Follow-up to this message
Old Post
Offer Kaye
04-20-05 08:56 PM


Re: WhiteSpace - Map, search replace, split
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 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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
04-21-05 01:56 AM


Re: WhiteSpace - Map, search replace, split
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!

Report this thread to moderator Post Follow-up to this message
Old Post
FreeFall
04-21-05 08:56 AM


Re: WhiteSpace - Map, search replace, split
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

Report this thread to moderator Post Follow-up to this message
Old Post
Offer Kaye
04-21-05 08:56 AM


Re: WhiteSpace - Map, search replace, split
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!

Report this thread to moderator Post Follow-up to this message
Old Post
FreeFall
04-21-05 01:55 PM


Re: WhiteSpace - Map, search replace, split
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

Report this thread to moderator Post Follow-up to this message
Old Post
Offer Kaye
04-21-05 01:55 PM


Re: WhiteSpace - Map, search replace, split
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!

Report this thread to moderator Post Follow-up to this message
Old Post
FreeFall
04-21-05 01:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:14 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.