Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

(code) Array Play

by ybiC (Prior)
on Apr 30, 2001 at 07:07 UTC ( #76539=snippet: print w/replies, xml ) Need Help??
Description: A nothing snippet illustrating a few rudimentary array capabilities.   Extra points for recognizing what book I've been re-reviewing {grin}

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

# 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);
  "    first entry:  $first\n",
  "    last entry:   $last\n",
  "    random entry: $words[$random]\n",

print "  as entered:\n";

print "  reverse order:\n";
my @reversed = reverse(@words);

print "  sorted alphabetically:\n";
my @sorted = sort(@words);

print "  reverse alphabetic:\n";
my @revalpha = reverse(@sorted);

if ($^O eq 'MSWin32') {
  print "  Hit <enter> to exit.";

sub PrintList {
  print "    $_\n" for(@_);
  print "\n";
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://76539]
[Lady_Aleena]: Corion, I knew that, I was just giving Discipulus a hard time.
choroba likes the story at the end of the node
[choroba]: reminds me of the days at the bank
[Corion]: Lady_Aleena: Hehe ;-D
[Lady_Aleena]: Corion, it was PerlMonks who kept pushing me to get Linux, so now I know some of the commands to use in word play.
[Corion]: Lady_Aleena: :-D

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2017-04-26 08:30 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (471 votes). Check out past polls.