Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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)?

Comment on Re^2: string diff and compare
Download Code
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2015-11-25 07:02 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (671 votes), past polls