Pure Programmer
Blue Matrix


Cluster Map

Lists

L1

This page is under construction. Please come back later.

Lists1.pl
#!/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]
Lists2.pl
#!/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
Lists3.pl
#!/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!
Lists4.pl
#!/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
Lists5.pl
#!/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

Projects

More ★'s indicate higher difficulty level.

References