Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Palindrome array

by LanX (Chancellor)
on Dec 29, 2012 at 06:13 UTC ( #1010793=note: print w/replies, xml ) Need Help??

in reply to Palindrome array

The ugly truth is both don't work!

eq forces scalar context and is meant to compare strings

So in the first code you are checking if the sizes (after stringification) are equal.

DB<107> @a = 1..3 => (1, 2, 3) DB<108> @a eq "3" => 1

in the second you have scalar from reverse on RHS

DB<123> (reverse @a) => (3, 2, 1) DB<124> scalar (reverse @a) => 321 DB<125> "321" eq (reverse @a) => 1

please stop doing weird things!

you can try the (less weird) smart match operator ~~ to compare arrays.

DB<129> @a=(3,1,3) => (3, 1, 3) DB<130> @a ~~ [reverse @a] => 1 DB<131> @a ~~ [3,1,3] => 1 DB<132> @a=1..3 => (1, 2, 3) DB<133> @a ~~ [reverse @a] => ""

a poor man's solution with eq is to explicitly stringify on both sides.

DB<143> @a=(3,1,3) => (3, 1, 3) DB<144> @ar=reverse @a => (3, 1, 3) DB<145> "@a" => "3 1 3" DB<146> "@a" eq "@ar" => 1

But this depends on the nature of your array elements, don't be too surprised about:

DB<151> "@a" => "3 1 3" DB<152> @b=(3,"1 3") => (3, "1 3") DB<153> "@a" eq "@b" => 1

Cheers Rolf

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1010793]
[Corion]: Hi Discipulus!
[marto]: hopefully you guys are also well :)
[Corion]: marto: Yeah - I'm looking for a week off in a week, even if it'll be a trip to Amsterdam ;)
[Discipulus]: oh thanks, i'm always at well as the ratchet or bucket
[Corion]: But I'm slowly starting to get into programming again - yesterday I translated my talk for YAPC::E (in Amsterdam) to English, and decided to put my Jekyll-clone to rest for the time being
[Corion]: Discipulus++ # being well is like being earnest
[marto]: hopefully next year I'll be in a position to go to Perly things in Europe :)
[Corion]: Maybe I'll use the Real Jekyll a bit to get a feel for how it actually works, and to get annoyed by it ;)
[Corion]: marto: That would be cool :) There is no kids care at Perly events unfortunately, so it's not entirely family friendly
[Corion]: I think I should define a set of (say) five projects through which I rotate from time to time, just to prevent myself from being bored/annoyed by the problems they get stuck in ;)

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2017-07-27 07:41 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (404 votes). Check out past polls.