LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-28-2010, 04:32 AM   #1
Evgeniy Arbatov
LQ Newbie
 
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 5

Rep: Reputation: 0
Perl and nested loop substitution


Hello! I have a trouble with getting Perl to substitute a regular expression in a nested loop. The construction is like this:

Code:
for ($i=11; $i<=99; $i++) {
        foreach my $line (@input) {
                if ($line =~ /10/) {
                        $line =~ s/10/$i/;
                        print $line . "\n";
                }
        }
}
However, the $i inside the foreach loop does not increment at the same time as $i in the for loop does. In fact, $i in the inner loop stays 11 until the for loop exits.

Please tell me, why this is the case? Many thanks.

Evgeniy
 
Old 04-28-2010, 05:05 AM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Your inner loop substitutes 11 for every 10 found in @input
So the next time the outer loop runs, and increments i, all the matches and substitutions have already been done,

There will never be a 10 to match in the inner loop after the first run. The if statement is never true. The printed i is always 11.

example containing 3 x 10, and showing the array after each pass of the inner loop.
Code:
#!/usr/bin/perl


@input = qw(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 10 98 15 13 10);

print "@input\n";

for ($i=11; $i<=20; $i++) {
        foreach my $line (@input) {
                if ($line =~ /10/) {
                        $line =~ s/10/$i/;
                        print $line . "\n";
                 }
        }
         print "iteration $i : @input\n";
}
Gives the output :

Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 10 98 15 13 10
11
11
11
iteration 11 : 1 2 3 4 5 6 7 8 9 11 11 12 13 14 15 16 17 18 19 20 11 98 15 13 11
iteration 12 : 1 2 3 4 5 6 7 8 9 11 11 12 13 14 15 16 17 18 19 20 11 98 15 13 11
iteration 13 : 1 2 3 4 5 6 7 8 9 11 11 12 13 14 15 16 17 18 19 20 11 98 15 13 11
iteration 14 : 1 2 3 4 5 6 7 8 9 11 11 12 13 14 15 16 17 18 19 20 11 98 15 13 11
iteration 15 : 1 2 3 4 5 6 7 8 9 11 11 12 13 14 15 16 17 18 19 20 11 98 15 13 11
iteration 16 : 1 2 3 4 5 6 7 8 9 11 11 12 13 14 15 16 17 18 19 20 11 98 15 13 11
iteration 17 : 1 2 3 4 5 6 7 8 9 11 11 12 13 14 15 16 17 18 19 20 11 98 15 13 11
iteration 18 : 1 2 3 4 5 6 7 8 9 11 11 12 13 14 15 16 17 18 19 20 11 98 15 13 11
iteration 19 : 1 2 3 4 5 6 7 8 9 11 11 12 13 14 15 16 17 18 19 20 11 98 15 13 11
iteration 20 : 1 2 3 4 5 6 7 8 9 11 11 12 13 14 15 16 17 18 19 20 11 98 15 13 11

Last edited by smoker; 04-28-2010 at 05:20 AM.
 
Old 04-28-2010, 05:19 AM   #3
Evgeniy Arbatov
LQ Newbie
 
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks a lot! I have confused myself
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested loop with 2 counters for 2 array variables briana.paige Linux - Newbie 2 06-19-2009 07:38 AM
bash scripting problem with nested if statements in while loop error xskycamefalling Programming 4 05-11-2009 03:14 PM
nested loop in Perl help needed Grafbak Programming 9 12-19-2006 10:28 AM
Substitution in Perl rigel_kent Programming 4 06-02-2006 10:11 AM
Nested-double loop error Harry Seldon Programming 3 05-06-2006 05:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration