MAP Question

Discussion in 'Perl Misc' started by George, Jan 17, 2005.

  1. George

    George Guest

    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.
     
    George, Jan 17, 2005
    #1
    1. Advertising

  2. George

    Matija Papec Guest

    X-Ftn-To: George

    "George" <> wrote:
    >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?


    --
    Matija
     
    Matija Papec, Jan 17, 2005
    #2
    1. Advertising

  3. George

    Anno Siegel Guest

    George <> wrote in comp.lang.perl.misc:
    > 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
     
    Anno Siegel, Jan 17, 2005
    #3
  4. George

    Peroli Guest

    hi george,
    If Map bugs you, why dont you try grep. It works.
    Peroli Sivaprakasam
     
    Peroli, Jan 18, 2005
    #4
  5. "Peroli" <> wrote in news:1106011879.674678.237640
    @z14g2000cwz.googlegroups.com:

    > 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.
     
    A. Sinan Unur, Jan 18, 2005
    #5
  6. On Mon, 17 Jan 2005 17:19:26 GMT, "George" <>
    wrote:

    >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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Jan 18, 2005
    #6
  7. George

    George Guest

    Michele Dondi wrote:

    > On Mon, 17 Jan 2005 17:19:26 GMT, "George" <>
    > wrote:
    >
    > > 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


    writing map as MAP was just for eye catcher, like in BOLD, I never
    thought you guys would even comment on that,
    any way
     
    George, Jan 18, 2005
    #7
  8. George

    George Guest

    Anno Siegel wrote:

    > George <> wrote in comp.lang.perl.misc:
    > > 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


    my mistake in understanding that RE was taking $_ as input ,
     
    George, Jan 18, 2005
    #8
  9. George

    Tintin Guest

    "George" <> wrote in message
    news:...
    > Michele Dondi wrote:
    >> > so I thought of using MAP(one liner) insted foreach.

    >>
    >> Not such a thing as MAP...

    >
    > 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.
     
    Tintin, Jan 18, 2005
    #9
  10. George

    George Guest

    Tintin wrote:

    >
    > "George" <> wrote in message
    > news:...
    > > Michele Dondi wrote:
    > >> > so I thought of using MAP(one liner) insted foreach.
    > > >
    > >> Not such a thing as MAP...

    > >
    > > 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.


    Makes sense,
    thanks for clarifying
     
    George, Jan 18, 2005
    #10
  11. George wrote:

    >>>@Rfolder=map {/#(.*?)(?=:)/,$_} @getlistF;


    > 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
     
    Brian McCauley, Jan 18, 2005
    #11
  12. George

    phaylon Guest

    Brian McCauley wrote:

    > 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

    --
    http://www.dunkelheit.at/
    I want, therefore I can.
     
    phaylon, Jan 18, 2005
    #12
  13. George

    George Guest

    Brian McCauley wrote:

    >
    >
    > George wrote:
    >
    > > > > @Rfolder=map {/#(.*?)(?=:)/,$_} @getlistF;

    >
    > > 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
     
    George, Jan 18, 2005
    #13
  14. On 17 Jan 2005 17:31:19 -0800, "Peroli" <> wrote:

    >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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Jan 18, 2005
    #14
  15. On Tue, 18 Jan 2005 13:05:07 GMT, "George" <>
    wrote:

    >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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Jan 19, 2005
    #15
  16. On 19 Jan 2005 10:27:45 +0100, Arndt Jonasson <>
    wrote:

    >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.
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Jan 19, 2005
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alex
    Replies:
    1
    Views:
    651
    Lau Lei Cheong
    Feb 4, 2005
  2. Matthias Hildebrand
    Replies:
    5
    Views:
    7,982
    krogers
    Mar 20, 2012
  3. Vlad
    Replies:
    0
    Views:
    362
  4. Patrick Guio
    Replies:
    6
    Views:
    3,210
    chris
    Oct 20, 2004
  5. Erik Arner
    Replies:
    0
    Views:
    1,313
    Erik Arner
    Nov 2, 2004
Loading...

Share This Page