To convert a set of strings to a set of floats in Perl, you can use the map
function to transform each string value into its float representation.
In this example,
@stringArray1
with values ("1.1", "2.2", "3.3")
.map
function on @stringArray1
to transform each string value into its corresponding float value. We pass a code block to map
that converts each string value to a float using the += 0
operator.@floatArray1
.@stringArray1 = ("1.1", "2.2", "3.3");
@floatArray1 = map { $_ += 0 } @stringArray1;
print join(", ", @floatArray1);
1.1, 2.2, 3.3
In this example,
@stringArray2
with values ("4.5", "5.6", "6.7")
.map
function on @stringArray2
to transform each string value into its corresponding float value. We pass a code block to map
that converts each string value to a float using the += 0
operator.@floatArray2
.@stringArray2 = ("4.5", "5.6", "6.7");
@floatArray2 = map { $_ += 0 } @stringArray2;
print join(", ", @floatArray2);
4.5, 5.6, 6.7
In this tutorial, we learned How to Convert a Set of Strings to a Set of Floats in Perl language with well detailed examples.