Lists
This page is under construction. Please come back later.
#!/usr/bin/env perl use utf8; use Utils; use strict; use warnings; MAIN: { my $NUM_SQUARES = 5; my $squares = []; # Put the squares into the list for (my $i = 0; $i < $NUM_SQUARES; ++$i) { push(@{$squares}, $i * $i); } # Print out the squares from the list for (my $i = 0; $i < scalar(@{$squares}); ++$i) { print Utils::messageFormat("\{0:d\}^2 = \{1:d\}", $i, $squares->[$i]), "\n"; } print Utils::listToString($squares), "\n"; }
Output
$ perl Lists1.pl
0^2 = 0
1^2 = 1
2^2 = 4
3^2 = 9
4^2 = 16
[0, 1, 4, 9, 16]
#!/usr/bin/env perl use utf8; use Utils; use strict; use warnings; MAIN: { my $squares = [0, 1, 4, 9, 16, 25]; # Print out the squares from the list for (my $i = 0; $i < scalar(@{$squares}); ++$i) { print Utils::messageFormat("\{0:d\}^2 = \{1:d\}", $i, $squares->[$i]), "\n"; } }
Output
$ perl Lists2.pl
0^2 = 0
1^2 = 1
2^2 = 4
3^2 = 9
4^2 = 16
5^2 = 25
#!/usr/bin/env perl use utf8; use strict; use warnings; MAIN: { my $names = ["Fred", "Wilma", "Barney", "Betty"]; foreach my $name (@$names) { print "Hello, ", $name, "!\n"; } $names = ["Harry", "Ron", "Hermione"]; foreach my $name (@$names) { print "Hello, ", $name, "!\n"; } }
Output
$ perl Lists3.pl
Hello, Fred!
Hello, Wilma!
Hello, Barney!
Hello, Betty!
Hello, Harry!
Hello, Ron!
Hello, Hermione!
#!/usr/bin/env perl use utf8; use Utils; use strict; use warnings; MAIN: { binmode(STDOUT, ":utf8"); binmode(STDERR, ":utf8"); binmode(STDIN, ":utf8"); foreach my $arg (@ARGV) { utf8::decode($arg); } # Print out the command line arguments for (my $i = 1; $i <= scalar(@ARGV); ++$i) { print $i, ":", $ARGV[$i - 1], "\n"; } }
Output
$ perl Lists4.pl Fred Barney Wilma Betty
1:Fred
2:Barney
3:Wilma
4:Betty
$ perl Lists4.pl 10 20 30 40
1:10
2:20
3:30
4:40
#!/usr/bin/env perl use utf8; use Utils; use strict; use warnings; MAIN: { binmode(STDOUT, ":utf8"); binmode(STDERR, ":utf8"); binmode(STDIN, ":utf8"); foreach my $arg (@ARGV) { utf8::decode($arg); } my $names = ["Fred", "Wilma", "Barney", "Betty"]; # Print out the name based on command line argument 1-4 for (my $i = 1; $i <= scalar(@ARGV); ++$i) { my $x = Utils::stoiWithDefault($ARGV[$i - 1], 0); print $i, ":", $names->[$x - 1], "\n"; } }
Output
$ perl Lists5.pl 1 2 3 4
1:Fred
2:Wilma
3:Barney
4:Betty
$ perl Lists5.pl 4 3 2 1
1:Betty
2:Barney
3:Wilma
4:Fred
$ perl Lists5.pl 1 3
1:Fred
2:Barney
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Array Last Element
- Central Limit Theorem
- Chromatic Scale Frequencies
- Income Tax (Revisited)
- Morse Code
- Renard Numbers (Preferred Numbers)
- Sample Mean and Standard Deviation
- Sieve of Eratosthenes
References
- [[Perl Language Reference]]
- [[Comprehensive Perl Archive Network (CPAN)]]
- [[Beginning Perl]], Simon Cozens
- [[Perl Monks Tutorials]]