Question

I wanted to print a statement in particular format. example:

my $flag = 0;

my $new_counter = 4;

print "hello world\n";

for ( my $i =0; $i le $new_counter; $i++) {
        print "flag = $i \n";
        }

the output i get here is

hello world
flag = 1 
flag = 2 
flag = 3 
flag = 4 

I want that the output to look like

hello world
flag = 1 
hello world
flag = 2 
hello world
flag = 3 
hello world
flag = 4 

Can someone help me with this?

Was it helpful?

Solution

Try:

my $flag = 0;

my $new_counter = 4;

for ( my $i =0; $i le $new_counter; $i++) {
    print "hello world\n";
    print "flag = $i \n";
}

The solution is to move print "hello world\n"; inside the for loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top