Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
A nothing snippet illustrating a few rudimentary array capabilities.   Extra points for recognizing what book I've been re-reviewing {grin}

Updates:
Slightly more idiomatic for(@list) syntax.
Cleaner OS-based break character detection (commented out), with ternary op syntax active.
Remove unecessary srand call.   Thanks tilly, for mentioning that.   8^)
Add Win32 detection for OS-specific prompting.
Move $random before $first and $last so all names included in random selection.   Thanks Albannach, for pointing that out.   8^)

#!/usr/bin/perl -w # arrayplay.pl # 2002-03-20 22:10 CST # 2001-04-30 use strict; # using ternary op: my $donechar = $^O =~ /MSWin32/ ? 'CTRL+Z<enter>' : 'CTRL+D'; print "\n Enter words, follow each with <enter>.\n"; print " $donechar after final <enter>.\n\n"; # same thing, but with default val+if: # my $donechar = 'CTRL+D'; # $donechar = 'CTRL+Z<enter>' if ($^O eq 'MSWin32'); # print "\n Enter words, follow each with <enter>.\n"; # print " $donechar after final word+<enter>.\n\n"; chomp(my @words = <STDIN>); print "\n ", $#words+1, " words entered.\n"; my $random = rand(@words); my $first = shift(@words); my $last = pop(@words); unshift(@words, $first); push(@words, $last); print " first entry: $first\n", " last entry: $last\n", " random entry: $words[$random]\n", "\n"; print " as entered:\n"; PrintList(@words); print " reverse order:\n"; my @reversed = reverse(@words); PrintList(@reversed); print " sorted alphabetically:\n"; my @sorted = sort(@words); PrintList(@sorted); print " reverse alphabetic:\n"; my @revalpha = reverse(@sorted); PrintList(@revalpha); if ($^O eq 'MSWin32') { print " Hit <enter> to exit."; <STDIN>; exit; } sub PrintList { print " $_\n" for(@_); print "\n"; }

In reply to (code) Array Play by ybiC

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 contemplating the Monastery: (5)
As of 2024-04-19 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found