Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: How to create loop in perl dynamically

by djerius (Beadle)
on Sep 26, 2012 at 13:38 UTC ( #995769=note: print w/ replies, xml ) Need Help??


in reply to How to create loop in perl dynamically

Since your loops are nested, you're really generating a cartesian product of the list indices; try Math::Cartesian::Product.


Comment on Re: How to create loop in perl dynamically
Re^2: How to create loop in perl dynamically
by adithi (Initiate) on Sep 26, 2012 at 14:12 UTC
    Not sure how to do this. But will try this.
      Your problem requires the following
      • The number of loops
      • The iteration ranges for each loop
      • Code which uses the loop indices
      You can encode the first two however you'd like; in the following example I've simply put the ranges in an array.

      You haven't provided an example of the code which runs inside the innermost loop. It might be useful if you provided some idea of what you're trying to do inside of those loops.

      I'm assuming that your code only needs to know the loop indices.

      Here's how you can use Math::Cartesian::Product:

      use Math::Cartesian::Product; # this encodes the number of loops and their ranges my @ranges = ( [ 0..1 ], [ 1..2 ], [2..3] ); # the code inside the braces is run for every combination # of the ranges cartesian { print "indices: @_\n"; } @ranges; # here's a version using a subroutine which sums up # the indices (just to do something with them) sub compute { my $sum = 0; $sum += $_ foreach @_; print "sum of @_ = $sum\n"; } cartesian \&compute, @ranges;
      And here are the results:
      indices: 0 1 2 indices: 0 1 3 indices: 0 2 2 indices: 0 2 3 indices: 1 1 2 indices: 1 1 3 indices: 1 2 2 indices: 1 2 3 sum of 0 1 2 = 3 sum of 0 1 3 = 4 sum of 0 2 2 = 4 sum of 0 2 3 = 5 sum of 1 1 2 = 4 sum of 1 1 3 = 5 sum of 1 2 2 = 5 sum of 1 2 3 = 6
        Thank you. That was helpful.
        This is requirement.
        1. I have data in DB
        ex: of data in DB.
        Student|Subject|Marks aaa|eur|100 aaa|usd|90 aaa|eur|101 aaa|usd|80 aaa|eur|90 --------- bbb|usd|80 bbb|usd|100
        .......
        So Mapping goes like below:
        aaa->eur->100 aaa->eur->101 aaa->eur->90 aaa->usd->90 aaa->usd->80

        Output will be : 1.If level=2
        aaa,eur,sum(100,101,90) aaa,usd,sum(90,80)
        so on..

        2.If level is 3, we have more detailed level
        aaa,eur,100,sum(gg) aaa,eur,100,sum(hh) aaa,eur,101,sum(gf) aaa,eur,101,sum(s) aaa,eur,90,sum(dd) aaa,eur,90,sum(sf) aaa,usd,90,sum(eq)
        ...
        Here its the users choice if u want to print at 2 level or 3 level or more..
        If level is 4 then we go into detailed level.

        Hope this requirement helps
        I want to dynamically store the values in arrays and iterate through these arrays and get this values.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://995769]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2014-12-26 09:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (169 votes), past polls