For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > Jumping to inner loops









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Jumping to inner loops
Luba Pardo

2006-10-24, 7:57 am

Dear all:
I need to write a script that, given that a statement in the "OUTER" loop is
true goes immediatly to the INNER loop and finishes it before iterates in
OUTER again.
The problem is that I do not how to ask the program to run the inner loop
once the statement of the loop "OUTER" is true.
Example: once that the element h=0 from arra1 is found in array2, then move
to the inner loop for h to become h+2 and not h+4. Then when the 4
consecutive elements in the array 1are found in the array 2, then make h=
h+4 in the array1. Elements in the other array should move also every 4
elements. I tried several options like next and redo, but I can not get what
I want. Hope someone can help.
Thanks in advance.
Luba

The script looks like:

use strict;
use warnings;
my @arra1=(1,2,3,4,5,6,7,8,9,10,11,12,13,14
,15,16,17,18,19,20,21,22,23,24);
my @arra2=
(0,0,0,0,0,0,0,0,0,0,0,0,"x","x",0,0,3,4,"d","e",1,2,"a","b",5,6,"l","c","x","x",0,0,7,8,"f","g",0,0,0,0,0,0,0,0,0,0,0,0,11,12,"n","o",13,14,"p","q",0,0,0,0,0,0,0,0,15,16,"r","s",21,22,"aa","bb",19,20,"cc","dd",17,18,"ee","ff",23,24,"gg","hh",21,22,"jj","
kk",9,10,"m","i");


my $k=0;


for ($h=0; $h<=$#arra1; $h= $h+4) {


OUTER: if ($arra2[$k]== $arra1[$h]&& $arra2[$k+1]== $arra1[$h+1] ||
$arra2[$k]== $arra1[$h+1]&& $arra2[$k+1]== $arra1[$h]) {
print " $arra2[$k] and $arra2[$k+1] give $arra2[$k+2] and
$arra2[$k+3]\n";
$k=0;


INNER: if ($arra2[$k]== $arra1[$h+2]&& $arra2[$k+1]== $arra1[$h+3] ||
$arra2[$k]== $arra1[$h+3]&& $arra2[$k+1]== $arra1[$h+2]){
print " $arra2[$k] and $arra2[$k+1] give $arra2[$k+2] and
$arra2[$k+3]\n";
$k=0;
}

} elsif($k<=$#arra2) {
$k=$k+4;
$h = $h-4;
}
}

DJ Stunks

2006-10-30, 7:03 pm

Luba Pardo wrote:
> Dear all:
> I need to write a script that, given that a statement in the "OUTER" loop is
> true goes immediatly to the INNER loop and finishes it before iterates in
> OUTER again.
> The problem is that I do not how to ask the program to run the inner loop
> once the statement of the loop "OUTER" is true.
> Example: once that the element h=0 from arra1 is found in array2, then move
> to the inner loop for h to become h+2 and not h+4. Then when the 4
> consecutive elements in the array 1are found in the array 2, then make h=
> h+4 in the array1. Elements in the other array should move also every 4
> elements. I tried several options like next and redo, but I can not get what
> I want. Hope someone can help.
> Thanks in advance.
> Luba
>
> The script looks like:
>
> use strict;
> use warnings;
> my @arra1=(1,2,3,4,5,6,7,8,9,10,11,12,13,14
,15,16,17,18,19,20,21,22,23,24);
> my @arra2=
> (0,0,0,0,0,0,0,0,0,0,0,0,"x","x",0,0,3,4,"d","e",1,2,"a","b",5,6,"l","c","x","x",0,0,7,8,"f","g",0,0,0,0,0,0,0,0,0,0,0,0,11,12,"n","o",13,14,"p","q",0,0,0,0,0,0,0,0,15,16,"r","s",21,22,"aa","bb",19,20,"cc","dd",17,18,"ee","ff",23,24,"gg","hh",21,22,"jj"

,"kk",9,10,"m","i");
>
>
> my $k=0;
>
>
> for ($h=0; $h<=$#arra1; $h= $h+4) {
>
>
> OUTER: if ($arra2[$k]== $arra1[$h]&& $arra2[$k+1]== $arra1[$h+1] ||
> $arra2[$k]== $arra1[$h+1]&& $arra2[$k+1]== $arra1[$h]) {
> print " $arra2[$k] and $arra2[$k+1] give $arra2[$k+2] and
> $arra2[$k+3]\n";
> $k=0;
>
>
> INNER: if ($arra2[$k]== $arra1[$h+2]&& $arra2[$k+1]== $arra1[$h+3] ||
> $arra2[$k]== $arra1[$h+3]&& $arra2[$k+1]== $arra1[$h+2]){
> print " $arra2[$k] and $arra2[$k+1] give $arra2[$k+2] and
> $arra2[$k+3]\n";
> $k=0;
> }
>
> } elsif($k<=$#arra2) {
> $k=$k+4;
> $h = $h-4;
> }
> }


I don't understand what you're trying to accomplish.

-jp

Charles K. Clarkson

2006-10-30, 7:03 pm

Luba Pardo <mailto:lubapardo@gmail.com> wrote:

: Example: once that the element h=0 from arra1 is found in
: array2, then move to the inner loop for h to become h+2
: and not h+4. Then when the 4 consecutive elements in the
: array 1are found in the array 2, then make h= h+4 in the
: array1. Elements in the other array should move also
: every 4 elements.

Can you explain that better? It really makes very
little sense. Ignore the script and just explain each step
you wish to take. Forget about h and k and the inner and
outer loop, just explain what you are trying to accomplish.


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
Free Market Advocate
Web Programmer

254 968-8328

http://www.clarksonenergyhomes.com/

Don't tread on my bandwidth. Trim your posts.

Rob Dixon

2006-10-30, 7:03 pm

Luba Pardo wrote:
> Dear all:
> I need to write a script that, given that a statement in the "OUTER"
> loop is
> true goes immediately to the INNER loop and finishes it before iterates in
> OUTER again.
> The problem is that I do not how to ask the program to run the inner loop
> once the statement of the loop "OUTER" is true.
> Example: once that the element h=0 from arra1 is found in array2, then
> move
> to the inner loop for h to become h+2 and not h+4. Then when the 4
> consecutive elements in the array 1are found in the array 2, then make h=
> h+4 in the array1. Elements in the other array should move also every 4
> elements. I tried several options like next and redo, but I can not get
> what
> I want. Hope someone can help.
> Thanks in advance.
> Luba
>
> The script looks like:
>
> use strict;
> use warnings;
> my @arra1=(1,2,3,4,5,6,7,8,9,10,11,12,13,14
,15,16,17,18,19,20,21,22,23,24);
> my @arra2=
>

(0,0,0,0,0,0,0,0,0,0,0,0,"x","x",0,0,3,4,"d","e",1,2,"a","b",5,6,"l","c","x","x",0,0,7,8,"f","g",0,0,0,0,0,0,0,0,0,0,0,0,11,12,"n","o",13,14,"p","q",0,0,0,0,0,0,0,0,15,16,"r","s",21,22,"aa","bb",19,20,"cc","dd",17,18,"ee","ff",23,24,"gg","hh",21,22,"jj","
kk",9,10,"m","i");

>
>
>
> my $k=0;
>
>
> for ($h=0; $h<=$#arra1; $h= $h+4) {
>
>
> OUTER: if ($arra2[$k]== $arra1[$h]&& $arra2[$k+1]== $arra1[$h+1] ||
> $arra2[$k]== $arra1[$h+1]&& $arra2[$k+1]== $arra1[$h]) {
> print " $arra2[$k] and $arra2[$k+1] give $arra2[$k+2] and
> $arra2[$k+3]\n";
> $k=0;
>
>
> INNER: if ($arra2[$k]== $arra1[$h+2]&& $arra2[$k+1]== $arra1[$h+3] ||
> $arra2[$k]== $arra1[$h+3]&& $arra2[$k+1]== $arra1[$h+2]){
> print " $arra2[$k] and $arra2[$k+1] give $arra2[$k+2] and
> $arra2[$k+3]\n";
> $k=0;
> }
>
> } elsif($k<=$#arra2) {
> $k=$k+4;
> $h = $h-4;
> }
> }


Hello Luba

My best guess is that you're looking for the program below, is that right? I
think it's a misconception to be stepping through the first array in fours, as
it seems to be a list of number pairs, but you don't say enough about the
problem for me to be sure so I've left it as it is. If I'm right, then the loop
can be reduced to just


for (my $h = 0; $h <= $#arra1; $h = $h + 2) {

my @h = @arra1[$h,$h+1];

for (my $k = 0; $k <= $#arra2; $k = $k + 4) {

my @k = @arra2[$k .. $k+3];

if ($k[0] eq $h[0] and $k[1] eq $h[1]
or $k[0] eq $h[1] and $k[1] eq $h[0]) {
printf " %s and %s give %s and %s\n", @k;
}
}
}

which gives identical output with the data you give.

I hope this helps.

Rob



use strict;
use warnings;

my @arra1 = 1 .. 24;
my @arra2 = qw(
0 0 0 0
0 0 0 0
0 0 0 0
x x 0 0
3 4 d e
1 2 a b
5 6 l c
x x 0 0
7 8 f g
0 0 0 0
0 0 0 0
0 0 0 0
11 12 n o
13 14 p q
0 0 0 0
0 0 0 0
15 16 r s
21 22 aa bb
19 20 cc dd
17 18 ee ff
23 24 gg hh
21 22 jj kk
9 10 m i
);


for (my $h = 0; $h <= $#arra1; $h = $h + 4) {

my @h = @arra1[$h .. $h+3];

for (my $k = 0; $k <= $#arra2; $k = $k + 4) {

my @k = @arra2[$k .. $k+3];

if ($k[0] eq $h[0] and $k[1] eq $h[1] or $k[0] eq $h[1] and $k[1] eq $h[0]) {
printf " %s and %s give %s and %s\n", @k;
}
}

for (my $k = 0; $k <= $#arra2; $k = $k + 4) {

my @k = @arra2[$k .. $k+3];

if ($k[0] eq $h[2] and $k[1] eq $h[3] or $k[0] eq $h[3] and $k[1] eq $h[2]) {
printf " %s and %s give %s and %s\n", @k;
}
}
}

**OUTPUT**

1 and 2 give a and b
3 and 4 give d and e
5 and 6 give l and c
7 and 8 give f and g
9 and 10 give m and i
11 and 12 give n and o
13 and 14 give p and q
15 and 16 give r and s
17 and 18 give ee and ff
19 and 20 give cc and dd
21 and 22 give aa and bb
21 and 22 give jj and kk
23 and 24 give gg and hh

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com