In Perl, you can convert a set of integers to a set of strings by iterating over each element of the integer set and converting it to a string using the ""
operator or the sprintf()
function.
In this example,
%intSet1
containing the elements 1, 2, 3, 4, 5
.%stringSet1
.%intSet1
using a foreach
loop.""
operator and add it to the %stringSet1
.%stringSet1
to standard output.use strict;
use warnings;
my %intSet1 = (1 => 1, 2 => 1, 3 => 1, 4 => 1, 5 => 1);
my %stringSet1 = ();
foreach my $num (keys %intSet1) {
$stringSet1{"$num"} = 1;
}
print "{" . join(", ", keys %stringSet1) . "}\n";
{1, 2, 3, 4, 5}
In this example,
%intSet2
containing the elements 10, 20, 30, 40, 50
.%stringSet2
.%intSet2
using a foreach
loop.sprintf()
function and add it to the %stringSet2
.%stringSet2
to standard output.use strict;
use warnings;
my %intSet2 = (10 => 1, 20 => 1, 30 => 1, 40 => 1, 50 => 1);
my %stringSet2 = ();
foreach my $num (keys %intSet2) {
$stringSet2{sprintf("%d", $num)} = 1;
}
print "{" . join(", ", keys %stringSet2) . "}\n";
{10, 20, 30, 40, 50}
In this tutorial, we learned How to Convert a Set of Integers to a Set of Strings in Perl language with well detailed examples.