Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Re (tilly) 1: (Golf) Strings-to-Array

by demerphq (Chancellor)
on Sep 01, 2001 at 04:56 UTC ( #109586=note: print w/replies, xml ) Need Help??

in reply to Re (tilly) 1: (Golf) Strings-to-Array
in thread (Golf) Strings-to-Array

So if I was a careless lout, I could claim 28, but I am not...
sub r {my$s;$s.=chop for@_=@_;$s.0?(&r,$s):()}

Tilly, I have tested your line as follows
sub r {my$s;$s.=chop for@_;$s?(&r,$s):()} #0 1 2 3 3 #123456789012345678901234567890123 # jeff,john,mary # jjm,eoa,fhr,fny
on v5.6.1-AS628. and it worked fine.

I suppose I am missing something, but I dont see what.
Why are you doing @_=@_ and $s.0??
Is this an issue for an earlier version of perl? As far as I understand these are redundant ops. When does $s.0 evalute in a boolean sense different to $s? And what is the point of assigning @_ to itself?
I am confused.

OTOH it does mean that you can claim 5 less chars, for a total of 33.
Or am I being a lout?


Replies are listed 'Best First'.
Re (tilly) 3: (Golf) Strings-to-Array
by tilly (Archbishop) on Sep 01, 2001 at 05:13 UTC
    In Perl 5.6.0 if you passed in literal strings, you got an error about, "Modification of a read-only value attempted". Recopying @_ fixed that.

    And the time when $s and $s.0 are different in boolean context is when $s is "0". So when you remove those two characters, you get a bug.

    An example that shows the bug:

    print map "$_\n", r("0a1", "b0", "0");
      Hi Tilly.

      Ok so on that input it doesnt work.

      I guess now I'm being a pain, but japhy's spec *did* say make a function that takes any number of equal-length strings

      However I do believe in defensive programming, so I can see why you did that.

      Very interesting, and food for thought


      $\="\n"; $,=","; print r("0000", "0000", "0000"); #outputs #000,000,000,000
        And what happens if you call r('0000')?

        Any number includes 1. :-)

      Hmm, and I reran it with literal params (I was using an @array) and I got that warnings as well. Interesting. OST. It follows from the fact @_ alises not copies, but still somehow surprising.

      And it is also very interesting to learn that '00' evals to true. Definitely *not* what I expected. I was thinking that it would fall under the evals to 0 rule, but now I see not. Thanx.

      On the other hand I tested a bunch of cases with 0's in them and I couldnt see how it affected the solution?


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://109586]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2020-09-28 18:42 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (144 votes). Check out past polls.