Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by jorg (Friar)
on Mar 30, 2001 at 15:46 UTC ( [id://68337]=note: print w/replies, xml ) Need Help??


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

tilly would you care to explain it as well at the risk of me producing this type of code on my next project?

Jorg

"Do or do not, there is no try" -- Yoda
  • Comment on Re: Re (tilly) 1: (Golf) Strings-to-Array

Replies are listed 'Best First'.
Re (tilly) 3: (Golf) Strings-to-Array
by tilly (Archbishop) on Mar 30, 2001 at 18:09 UTC
    Please don't produce this kind of code except in fun.

    But since you ask, the trick is recursion. Here it is broken out:

    sub r { my $s; $s .= chop for @_ = @_; $s . 0 ? ( &r , $s ) : () }
    and now with better variable names, fewer implicit variables, rewritten loops, that kind of thing:
    sub recursive_s2a { my $str_last; foreach my $str (@_ = @_) { $str_last .= chop($str); } if ($str_last . 0) { return (recursive_s2a(@_) , $str_last ); } else { return; } }
    And now you see that the function creates the last string in the list to produce, tests whether that was the empty string, if it was it returns nothing, if it wasn't it then proceeds by recursion to generate the first strings, and adds the string it produced to that list.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2024-04-19 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found