Pure Programmer
Blue Matrix


Cluster Map

Selection

L1

This page is under construction. Please come back later.

Selection1.pl
#!/usr/bin/env perl
use utf8;
use strict;
use warnings;

MAIN:
{
	print "Start...\n";
	if (scalar(@ARGV) != 2) {
		print "You need at least two command line arguments!\n";
	}
	print "Finish...\n";
}

Output
$ perl Selection1.pl abc Start... You need at least two command line arguments! Finish... $ perl Selection1.pl abc 123 Start... Finish...
Selection2.pl
#!/usr/bin/env perl
use utf8;
use Utils;
use strict;
use warnings;

MAIN:
{
	my $x = Utils::stoiWithDefault($ARGV[0], 0);
	print "Start...\n";
	if ($x > 10) {
		print "Greater than 10\n";
	} else {
		print "Less than or equal to 10\n";
	}
	print "Finish...\n";
}

Output
$ perl Selection2.pl 8 Start... Less than or equal to 10 Finish... $ perl Selection2.pl 12 Start... Greater than 10 Finish...
Selection3.pl
#!/usr/bin/env perl
use utf8;
use Utils;
use strict;
use warnings;

MAIN:
{
	my $x = Utils::stoiWithDefault($ARGV[0], 0);
	print "Start...\n";
	if ($x > 1000) {
		print "Greater than 1000\n";
	} elsif ($x > 100) {
		print "Greater than 100 but less than or equal to 1000\n";
	} else {
		print "Less than or equal to 100\n";
	}
	print "Finish...\n";
}

Output
$ perl Selection3.pl 12 Start... Less than or equal to 100 Finish... $ perl Selection3.pl 120 Start... Greater than 100 but less than or equal to 1000 Finish... $ perl Selection3.pl 1200 Start... Greater than 1000 Finish...
The switch construct is not a built-in part of Perl. You will need to install the CPAN module Switch to use it.
$ sudo CPAN install Switch
Selection4.pl
#!/usr/bin/env perl
use utf8;
use Utils;
use strict;
use warnings;

MAIN:
{
	my $x = Utils::stoiWithDefault($ARGV[0], 0);
	if ($x == 1) {
		print "One\n";
	} elsif ($x == 2) {
		print "Two\n";
	} elsif ($x == 3 || $x == 4) {
		print "Three or Four\n";
	} else {
		print "Just don't know!\n";
	}
}

Output
$ perl Selection4.pl 0 Just don't know! $ perl Selection4.pl 1 One $ perl Selection4.pl 2 Two $ perl Selection4.pl 3 Three or Four $ perl Selection4.pl 4 Three or Four
Selection5.pl
#!/usr/bin/env perl
use utf8;
use Utils;
use strict;
use warnings;

MAIN:
{
	my $x = substr($ARGV[0], 0, 1);
	$x = lc($x);
	if ($x eq "a" || $x eq "e" || $x eq "i" || $x eq "o" || $x eq "u") {
		print Utils::messageFormat("\{0:c\} is a vowel", ord($x)), "\n";
	} elsif ($x eq "y") {
		print "y is sometimes a vowel\n";
	} else {
		print Utils::messageFormat("\{0:c\} is a consonant", ord($x)), "\n";
	}
}

Output
$ perl Selection5.pl a a is a vowel $ perl Selection5.pl g g is a consonant $ perl Selection5.pl I i is a vowel $ perl Selection5.pl T t is a consonant $ perl Selection5.pl y y is sometimes a vowel
perl

Questions

Projects

More ★'s indicate higher difficulty level.

References