Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2018-05-22 01:36 GMT
Find Nodes?
    Voting Booth?