Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I'd suggest creating a function (or an object) that returns the next term in your requested series. The secret to laziness is an iterator.
{ my $init = 'aa'; my $start = 'a'; my $len = length $init; sub next_term { my $ret = $init; my $p = $len - 1; while (1) { substr($init, $p, 1) =~ tr/acgnt/cgnta/; last if substr($init, $p--, 1) ne $start; $init = "$start$init", $len++, last if $p < 0; } return $ret; } }
You could also mimic this with a tied scalar, and have the FETCH function do what I have done above.
package Tie::ScalarIter; sub TIESCALAR { my ($class, $init, $start) = @_; bless [ $init, $start, length($init) ], $class; } sub FETCH { my $s = shift; my $ret = $s->[0]; my $p = $s->[2] - 1; while (1) { substr($s->[0], $p, 1) =~ tr/acgnt/cgnta/; last if substr($s->[0], $p--, 1) ne $s->[1]; $s->[0] = "$s->[1]$s->[0]", $s->[2]++, last if $p < 0; } return $ret; } sub STORE { my $s = shift; my $rep = shift; if (ref $rep) { @$s = (@$rep, length $rep->[0]) } else { (@$s[0,2] = ($rep, length $rep) } } 1;
Both interfaces are simple to use:
# functional my @strings; while (defined (my $next = next_term())) { last if length($next) > 4; push @strings, $next; } # tied scalar use Tie::ScalarIter; tie my($iter), 'Tie::ScalarIter', 'aa', 'a'; my @strings; while (defined (my $next = $iter)) { last if length($next) > 4; push @strings, $next; }
And that's about it.

Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

In reply to Re: Let's get lazy by japhy
in thread Let's get lazy by guillaume

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and !@monks...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (4)
    As of 2018-04-24 23:14 GMT
    Find Nodes?
      Voting Booth?