Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^2: string diff and compare

by raybies (Chaplain)
on May 04, 2012 at 15:36 UTC ( #968946=note: print w/replies, xml ) Need Help??

in reply to Re: string diff and compare
in thread string diff and compare

So I've gone through your example now. I understand everything in it, except...

 my ( $rold, $rnew ) = map scalar reverse, $old, $new;

What's up with the map scalar reverse syntax? What does the scalar do for example? I mean, I get that you're reversing two strings and putting them into two new strings, but I am really having difficulty parsing how Perl reads this to do what you've done here.

Anyone care to explain it, decompress it (for us mortals)?

Replies are listed 'Best First'.
Re^3: string diff and compare
by Anonymous Monk on May 04, 2012 at 15:54 UTC

    From perlod ...

    reverse LIST
    In list context, returns a list value consisting of the elements of LIST in the opposite order. In scalar context, concatenates the elements of LIST and returns a string value with all characters in the opposite order.

    And, read map chain as: map { scalar( reverse $_ ) } ( $old , $new ).

      "perlod"? Ha! :-) sounds like "perllord" was misspelled.
Re^3: string diff and compare
by jwkrahn (Monsignor) on May 04, 2012 at 21:27 UTC
    my ( $rold, $rnew ) = map scalar reverse, $old, $new;

    Is just a different way of saying:

    my $rold = reverse $old; my $rnew = reverse $new;

    You need to use scalar because reverse is in list context in the map expression.

      There are other ways of enforcing scalar context which are a lot more fun. :-)

      use 5.010; use strict; use warnings all => 'FATAL'; my ( $old, $new ) = qw( old new ); my ( $rold, $rnew ) = map~~reverse, $old, $new; say for "$old => $rold", "$new => $rnew";
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968946]
[marto]: yep :)
[Discipulus]: being her marto, can you explain what "Par for the course I'm afraid." means?
Discipulus here..
[marto]: "what is normal or expected in any given circumstances."
[marto]: better: "If a type of behaviour, event, or situation is par for the course, it is not good but it is normal or as you would expect"
[Discipulus]: thanks i was unable to decide where to split the sentece
[marto]: FWIW search.cpan rarely has issues, see http://noc.perl. org for a route to report problems
[oakbox]: thanks, marto.
[marto]: there was a period where search.cpan had some frequent outages for (IIRC) a couple of weeks. I've not had any problems since, until today.
[marto]: this was about 16 months ago maybe. the noc team are, in my experience, very responsive to reports of issues, so please raise the issue after checking known problems/outages

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2017-07-26 10:14 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (388 votes). Check out past polls.