473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

perl recursive iteration

89
Hi,

I have the data as below:

1,1000,2000,3000 999,1999,2999,3999 [m1,m2,m3,m4 n1,n2,n3,n4]
And the number of 1st and 2nd set will vary in the every line but there will be equal number of numbers in both the set.

I am successful in collecting them in the following simple manner as below:

999-1000, 1999-2000, 2999-3000 [n1-m2, n2-m3, n3-m4]

Basically, I collect all the start nos as 1,1000,2000 and 3000 and end nos in 999,1999,2999,3999 two arrays.
Expand|Select|Wrap|Line Numbers
  1. $starts = "1,1000,2000,3000";
  2. $ends = "999,1999,2999,3999";
  3.  my @test_starts = split (/,/, $starts);
  4.     my @test_ends    = split (/,/, $ends);
  5.  
  6.  $size= @starts - 1;
  7. for (my $i = 0; $i < $size; $i++)
  8.     {
  9.        print "$test_end[i] - $test_start[i+1]\n";
  10.  
  11.     }
  12.  
  13.  
The above mentioned script is just showing the method I have used to show my results and might contain some typos as these are not my actual code. And it works till this.

Now I want to add all the possible ways as below:

999-1000, 1999-2000, 2999-3000 [n1-m2, n2-m3, n3-m4]
as well as
999-2000, 999-3000 [n1-m3, n1-m4]

I am bit confused as this might require any recursive. Please let me know any method to solve the issue. Thanks.
Sep 23 '09 #1
2 2114
toolic
70 Expert
I think you just need an inner loop, instead of recursion.
Does this give you the output you expect?
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my @starts = qw(1   1000 2000 3000);
  5. my @ends   = qw(999 1999 2999 3999);
  6. for my $i (0 .. (@starts-2)) {
  7.     for my $j (0 .. (@starts-2)) {
  8.         print "$ends[$i] - $starts[$j+1]\n" if $starts[$j+1] > $ends[$i];
  9.     }
  10. }
  11.  
  12. __END__
  13.  
  14. 999 - 1000
  15. 999 - 2000
  16. 999 - 3000
  17. 1999 - 2000
  18. 1999 - 3000
  19. 2999 - 3000
  20.  
Sep 23 '09 #2
lilly07
89
Thanks Toolic and that did help me.
Sep 29 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Paul Chiusano | last post by:
I've been playing around with generators and have run into a difficulty. Suppose I've defined a Node class like so: class Node: def __init__(self, data=None, left=None, right=None):...
19
by: Carlos Ribeiro | last post by:
Hello all, Here I am using some deeply nested, tree-like data structures. In some situations I need to traverse the tree; the old-style way to do it is to write a recursive method on the node...
0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
3
by: Robert Song | last post by:
Hi all I am implementing a stored procedure which needs to recursively call itself until specific condition is reached, Could anyone give some advice about that? Thanks a lot Robert Song
2
by: LoserInYourFaceEngineer | last post by:
Hello All: I'm having trouble with a recursive function. The function is supposed to identify nested folders in a hierarchical folder structure. The function "searchForFolders()" is...
1
by: Matt | last post by:
Hi all :) I'm trying to get the functionality gained using only CSS in Opera/Gecko etc on this page: <http://matt.blissett.me.uk/web/authoring/css_menus/sample> to work in IE, using javascript....
64
by: dmattis | last post by:
I am trying to write a recursive version of Power(x,n) that works by breaking n down into halves(where half of n=n/2), squaring Power(x,n/2), and multiplying by x again if n was odd, and to find a...
41
by: Harry | last post by:
Hi all, 1)I need your help to solve a problem. I have a function whose prototype is int reclen(char *) This function has to find the length of the string passed to it.But the conditions...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.