t-rex has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am facing a code problem , here the file 1 is yaml file whihc i parse and get a handle to it from where i have extracted the parameters and use it for some logic and further processing.

standalone_execution: - utpsm_executable: &seid_01 name: tpsm_BE rulesfile: [*rulesid_02,*rulesid_02,*rulesid_01] target_list: [*runid_01,*runid_03] - utpsm_executable: &seid_02 name: tpsm_LE rulesfile: [*rulesid_01,*rulesid_01] target_list: [*runid_02,*runid_01]

here the rulesid_01, runid_01... these are references in yaml file. this is how take the parameters from the yaml file

for my $seindex (@$standalone_exec) { $se_exec_name[$k] = $utpsm_exec[$k]->{name}; $se_tgt_list[$k] = $utpsm_exec[$k]->{target_list}; $se_rulesfile[$k] = $utpsm_exec[$k]->{rulesfile}; #$temp = $se_rulesfile[$k][0]; #$temp2 = $temp->{name}; for my $rindex (@{$se_rulesfile[$k]}) { $se_rules_name[$j] = $rindex->{name}; $se_rules_hostname[$j] = $rindex->{hostname}; $se_rules_path[$j] = $rindex->{path}; $j++; } for my $run_index (@{$se_tgt_list[$k]}) { $se_run_target_controls[$i] = $run_index->{target_controls +}; $se_run_target_types[$i] = $se_run_target_controls[$i]->{r +un_target_type}; $se_run_target_active[$i] = $se_run_target_controls[$i]->{ +run_target_active}; # print "se run wala : $i++; } #print "$se_run_target_username[0]\n"; $k++; }

now i want to have such logic that : for every runid all the rulesid should run ... so basically i want for element in targetlist array i use each element in rules file array in rules file.... so i write this code :

my $i = 0; my $j = 0; my $k =0; foreach my $se_index (@{$Yaml_Lib::standalone_exec}) { foreach my $se_runid (@{$Yaml_Lib::se_tgt_list[$i]}) { print " run target : $Yaml_Lib::se_run_target_username +[$j]\n"; foreach my $rindex ( @{$Yaml_Lib::se_rulesfile[1]} ) { print "rules index = $rindex\n"; print "$Yaml_Lib::se_rules_name[$k]\n"; $k++; } $j++; } $i++; }

when i take the loops separately that means for target list and rules file i get the elements individually, but my purpose is to make the pone element in target list to run for all elements in rules file.Please let me know what is wrong with this code

Replies are listed 'Best First'.
Re: array of array member access
by NetWallah (Canon) on Aug 08, 2016 at 20:41 UTC
    I'd like to help on this, but without runnable sample code, and in the absence of visibility into the data structure in memory, it is difficult to devine what the issue might be.

    Your code tends to use C-style loops, with parallel indices incrementing. This is somewhat error-prone. As an alternative, I'd suggest:

    • Use idomatic perl style loops
    • Use perl Classes/Objects as containers for these complex structures
    • Give us a better idea of "what's wrong"
    • Give us a better idea of what the objective is, and how this is supposed to work, and what the end result is expected to look like.

            "Software interprets lawyers as damage, and routes around them" - Larry Wall

      Hello, thanks for the reply, but the entire code is too big to be pasted, thats y i have pasted the code which i think can be useful , nevertheless if u wish i can put the entire code.

      also i figured that the problem lies in my data structures , the way i take the references ( since i am new to perl mostly i have coded in c and assembly so maybe it will take some time to get used to this).

      basically for each of my members in target list i have to run all the elements of rulesfile referenced i.e.

      for eg: rulesfile: [*rulesid_02,*rulesid_01] target_list: [*runid_01,* runid_04]

      so here runid_01 should run with rulesid_02 as well as rulesid_01 , similarly runid_04 should run with rulesid_02 as well as rulesid_01

      where rulesfile pointing to respective fields is rules_file: - rules_file_id: &rulesid_02 name: tpsm_rulesfile_BE hostname: - rules_file_id: &rulesid_03 name: test3 hostname: - rules_file_id: &rulesid_04 name: test4 hostname: and target_list referes to : run_target_platforms: - run_target_id: &runid_01 target_controls: run_target_type: simulator #simulator, emulator , hardware run_target_active: yes target_connection_info: run_target_hostname: - run_target_id: &runid_02 target_controls: run_target_type: hardware #simulator, emulator , hardware run_target_active: yes target_connection_info: run_target_hostname: NA

      I hope you see the rpoblem lies in my taking these data structures. i have deleted some fields to avoid complication

        OO-style programming can help overcome some of the complexities you are experiencing.

        From what you have described, I see 2 primary objects:
        * Rule
        * Target

        Each of these will have properties and methods.

        You could create other object classes that handle collections of these base objects, but initially, I'd suggest you keep that in your main code.

        Inside the "Target" class, you can have a method to run a single RULE, and another method to run an array or RULEs. (Or you could reverse that, an have a RULE run on a set of Targets).

        Once the basic underlying mechanics are setup, you can work on arrays of these objects.

        You will not regret the decision to invest in learing the OO way. And we monks are here to help.

                "Software interprets lawyers as damage, and routes around them" - Larry Wall