MAP Question

G

George

Hi
I needed to run regular exression(RE) for all the elements of an array
so I thought of using MAP(one liner) insted foreach.
Following line
@Rfolder=map {/#(.*?)(?=:)/,$_} @getlistF;
gave me all the existing elements plus what is extracted by RE

but
following line
@Rfolder=map {/#(.*?)(?=:)/} @getlistF;
gave me just what RE returned,

Definetly it means that I do not understand MAP correctly,
can somebody please explain what is the difference between two
statement , cause what I understood that $_ needs to be set before ,
for RE to work.
 
M

Matija Papec

X-Ftn-To: George

George said:
Hi
I needed to run regular exression(RE) for all the elements of an array
so I thought of using MAP(one liner) insted foreach.
Following line
@Rfolder=map {/#(.*?)(?=:)/,$_} @getlistF;
gave me all the existing elements plus what is extracted by RE

but
following line
@Rfolder=map {/#(.*?)(?=:)/} @getlistF;
gave me just what RE returned,

Definetly it means that I do not understand MAP correctly,
can somebody please explain what is the difference between two
statement , cause what I understood that $_ needs to be set before ,
for RE to work.

You didn't say what do you want to accomplish, and I'm not sure what are
you're expectations regarding map?
 
A

Anno Siegel

George said:
Hi
I needed to run regular exression(RE) for all the elements of an array
so I thought of using MAP(one liner) insted foreach.
Following line
@Rfolder=map {/#(.*?)(?=:)/,$_} @getlistF;
gave me all the existing elements plus what is extracted by RE

but
following line
@Rfolder=map {/#(.*?)(?=:)/} @getlistF;
gave me just what RE returned,

Definetly it means that I do not understand MAP correctly,
can somebody please explain what is the difference between two
statement , cause what I understood that $_ needs to be set before ,
for RE to work.

Have you read the map documentation (not "MAP", case matters)?

As it explains, the block you give to map is evaluated in list context,
with $_ set to the list elements in turn.

In the first case, the block returns whatever the regex extracts, followed
by $_. In the second case, there is no $_ in the block, so it returns
just what the regex extracts. What else is to explain?

Anno
 
A

A. Sinan Unur

hi george,
If Map bugs you, why dont you try grep. It works.

map (not Map) works too. map and grep do different things. See

perldoc -f map
perldoc -f grep

Sinan.
 
M

Michele Dondi

I needed to run regular exression(RE) for all the elements of an array
Yes...

so I thought of using MAP(one liner) insted foreach.

Not such a thing as MAP...
Following line
@Rfolder=map {/#(.*?)(?=:)/,$_} @getlistF;

....as your correct use of map() shows.
gave me all the existing elements plus what is extracted by RE

is this what you want? Is this _not_ what you want? What do you want?
but
following line
@Rfolder=map {/#(.*?)(?=:)/} @getlistF;
gave me just what RE returned,
Yes...

Definetly it means that I do not understand MAP correctly,

Perhaps... are you missing a list context here?

Definitely you do _not_ understand that there's not such a thing as
MAP. And I can't really understand why you spell it so.
can somebody please explain what is the difference between two
statement , cause what I understood that $_ needs to be set before ,
for RE to work.

What does make you suppose it's not set?

However I can't explain you the difference, but hopefully

perldoc -f map

can.


Michele
 
G

George

Michele said:
Not such a thing as MAP...


...as your correct use of map() shows.


is this what you want? Is this not what you want? What do you want?


Perhaps... are you missing a list context here?

Definitely you do not understand that there's not such a thing as
MAP. And I can't really understand why you spell it so.


What does make you suppose it's not set?

However I can't explain you the difference, but hopefully

perldoc -f map

can.


Michele

writing map as MAP was just for eye catcher, like in BOLD, I never
thought you guys would even comment on that,
any way
 
G

George

Anno said:
Have you read the map documentation (not "MAP", case matters)?

As it explains, the block you give to map is evaluated in list
context, with $_ set to the list elements in turn.

In the first case, the block returns whatever the regex extracts,
followed by $_. In the second case, there is no $_ in the block, so
it returns just what the regex extracts. What else is to explain?

Anno

my mistake in understanding that RE was taking $_ as input ,
 
T

Tintin

George said:
writing map as MAP was just for eye catcher, like in BOLD, I never
thought you guys would even comment on that,
any way

The reason it was commented on, is that a number of Perl newbies don't
realise that Perl is a case sensitive language and they often make silly
mistakes like:

use cgi;

instead of:

use CGI;

If someone talks about using the xyz module (instead of XYZ), or the ABC
function (instead of abc), it put doubt into peoples mind as to whether the
poster really knows the correct case.
 
G

George

Tintin said:
The reason it was commented on, is that a number of Perl newbies
don't realise that Perl is a case sensitive language and they often
make silly mistakes like:

use cgi;

instead of:

use CGI;

If someone talks about using the xyz module (instead of XYZ), or the
ABC function (instead of abc), it put doubt into peoples mind as to
whether the poster really knows the correct case.

Makes sense,
thanks for clarifying
 
B

Brian McCauley

my mistake in understanding that RE was taking $_ as input ,

You are the second person[1] in a month to have been suffering from the
fancifull belief that the comma operator has the semantics of the =~
operator but with the operands reversed.

It seems improbable that two people could arrive a such an odd
misconception independantly.

Can you please tell us the source of this disinformation so that we can
stamp on it.

[1]
http://groups-beta.google.com/group/comp.lang.perl.misc/msg/c315aa4228894ecd
 
P

phaylon

Brian said:
It seems improbable that two people could arrive a such an odd
misconception independantly.

Not? Without an eye for the perl-look it might seem that C<map> has two
arguments in your example ( map{/RE/,$_} !-> map(/RE/,$_) ).

I would file it under a bit too speedy reading..


p
 
G

George

Brian said:
my mistake in understanding that RE was taking $_ as input ,

You are the second person[1] in a month to have been suffering from
the fancifull belief that the comma operator has the semantics of the
=~ operator but with the operands reversed.

It seems improbable that two people could arrive a such an odd
misconception independantly.

Can you please tell us the source of this disinformation so that we
can stamp on it.

[1]
http://groups-beta.google.com/group/comp.lang.perl.misc/msg/c315aa4228
894ecd

I am already embarrassed for asking such a stupid question, now you are
asking me to undress :),
I read the article but there is difference:
Here I did not understood the map properly so I thought that $_ has to
be needs to be defined inside the block/EXPR for regex to perform
 
M

Michele Dondi

hi george,
If Map bugs you, why dont you try grep. It works.

map() and grep() do different things, even if they're indeed cousins
each other. There's nothing in the OP's post that suggests he may be
interested in the latter, but of course it would be important to
understand what he does really want or what he expected out of map().

As a side note I tried to draw the attention of the OP to the
inutility spelling "map" incorrectly only to, presumably, emphasize
it. I don't see no need to capitalize it either, nay, to be fair it
seems even worse than uppercasing it altogether.


Michele
 
M

Michele Dondi

Here I did not understood the map properly so I thought that $_ has to
be needs to be defined inside the block/EXPR for regex to perform

Not intended to be a personal offence, but... quite an interesting
"syntax"! ;-)

WRT your original post, $_ _is_ defined inside the block, _implicitly_
(which is the good point about it, BTW).


Michele
 
M

Michele Dondi

To add to this subject (as relative Perl newbie), some common ways of
pointing out that a word (or longer construct) in running text is not
meant as an ordinary English word, but rather as Perl code, are:

Funny, I felt the need to clarify the point too. I'm reading news
offline, so I hadn't read your reply yet, when writing mine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top