Counting to Scalar Value
Domanda
So I have a foreach
loop:
foreach (1..10) {
print "#", $t, "\n";
}
But I also have a scalar:
$number = 5;
Can I count from 1 to the scalar like so?
foreach (1..$number) {
print "#", $t, "\n";
}
When I do this, the program simply outputs nothing. What can I do to make this work?
Soluzione
Script:
#!/usr/bin/perl
use strict;
use warnings;
my $number = 5;
foreach (1..$number) {
print "#$_\n";
}
or
#!/usr/bin/perl
use strict;
use warnings;
my $number = 5;
print "#$_\n" for 1..$number;
Output:
#1
#2
#3
#4
#5
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow