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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

In a recent discussion, a friend told me 'she was glad I was glad', to which I (of course) immediately replied that 'I was glad she was glad I was glad'. One thing led to another (well, not that....) and voila:

#! /usr/bin/perl use strict; my $limit = $ARGV[0] || 10; my $index = 0; my $result = "I am glad."; $result = ( "I am", "you are" )[ $index = not $index ] . " glad " . $result foreach ( 1..$limit ); print $result, "\n";

This thing takes an integer from the command line and writes a (possibly ridiculously long) string saying 'I am glad you are glad I am glad...' eg.

c:/Data $ perl fn.pl 20 I am glad you are glad I am glad you are glad I am glad you are glad I + am glad you are glad I am glad you are glad I am glad you are glad I + am glad you are glad I am glad you are glad I am glad you are glad I + am glad you are glad I am glad.

The point here is the little list that toggles between 'I am' and 'you are'. The above snippet solves this nicely, but I find it noisy. Having read something about closures recently I then came up with the following:

#! /usr/bin/perl use strict; my $limit = $ARGV[0] || 10; my $result = "jeg er glad."; while( my $head = &swap ) { $result = $head. " er glad " . $result; } print $result, "\n"; BEGIN { my @oss = qw( jeg du ); my ( $index, $count ); sub swap{ ( $limit > $count++ ) ? return $oss[ $index = not $index ] : return; } }

The code does the same (although the result is in norwegian), and I like this better as it results in only one word where I want the toggle. As a downside, the code's "framework" is extremely verbose.

How does one toggle between two values in a simple and elegant way? Not too long-winded? Not too obscure?

As a bonus question, why doesn't ...

$result = $head. " er glad " . $result while( my $head = &swap );

... work under strict? It works with $head predeclared, but I'd rather not as one-liners are more neat. It seems to me that a statement like the above would define $head in the preceding line. Does a my statement inside a condition only apply to the following block? Not the logical "preceding block" to which the conditional is linked?

As a side note, I forgot why I named the variable $head. Now it makes sense, though, if only I knew why.

Cheers!


In reply to Toggling between two values by pernod

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

      No recent polls found