Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top