To create a set of numbers from 1 to N in Perl, you can use a hash where the keys represent the numbers.
In this example,
%numberSet
.1
in the hash.use strict;
use warnings;
my %numberSet;
for my $i (1..5) {
$numberSet{$i} = 1;
}
print "Set of numbers from 1 to 5:", join(", ", keys %numberSet), "\n";
Set of numbers from 1 to 5: 1, 2, 3, 4, 5
In this example,
%numberSet
.1
in the hash.use strict;
use warnings;
my %numberSet;
for my $i (1..10) {
$numberSet{$i} = 1;
}
print "Set of numbers from 1 to 10:", join(", ", keys %numberSet), "\n";
Set of numbers from 1 to 10: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
In this tutorial, we learned How to create a Set of Numbers from 1 to N in Perl language with well detailed examples.