in reply to access array of values without a loop

I would like to get a way where I can access the multiple values without looping.

By definition, that's impossible. It doesn't have to be a foreach loop, but a loop will be involved.

Why don't you say what you want to do? Would join be of use?

Replies are listed 'Best First'.
Re^2: access array of values without a loop
by spie287 (Novice) on Feb 20, 2015 at 12:35 UTC
    Hi All, Thanks for all your comments. I think I have more questions than I initially had. Which is good ! Now I understand the XY problem. I have a database which has 12 tables. The keys are column names from the excel sheet. This is my primary table and the rest in one to many relationship with this table. I have one value for the primary column in my LI table(COLUMN NAME LI) and can have multiple values for different columns in the same or different table.
    my $li_insert = "insert into LI values(LI_SEQ.nextval,?,?,?,?,?,?,?,? +,?,sysdate,?,?,?,?)"; my $mechanism_insert = "insert into mechanism (TAGET,ACTIONTYPE,ID,LI_ +ID) values(?,?,MECH_SEQ.nextval,LI_SEQ.currval)";
    Here one LI_ID can have multiple action_type or TAGET. So I want my script to be able to insert
    5,2,1,12 6,1,2,12.
    The data would be like this:
    { 'taget '=> [5,6], 'Action_type' =>[2,1] }
    in the excel sheet it actually comes as joined with a pipe operator I split them to have an array in a subroutine. How could I achieve this type of insertion if I keep them joined? Hope this is clear now!Let me know if you need any more details.
    # to get each hash from an array of hash foreach my $results(@$hash_of_excel){ my $specificresults = GroupResults($results); } sub GroupResults{ #to getonly the relevant columns(excel) for each table in the database +. #%details is a global hash tha holds different tables and their corres +ponding column my( $AllRecords )= @_; my %specificResults; for my $info (keys %details){ $specificResults{$info}{$_} = $AllRecords->{$_} for @{$details{$in +fo}; } } return \%specificResults; } sub SomethingUnPipeModifyRecord { #convert the joined values to array of values my( $rec ) = @_; for my $val( values %$rec ){ my $newval = [ split /\|/, $val ]; $val = $newval; } return $rec; } sub insertLI{ my ($li_results )= @_; my $li_array_values = SomethingUnPipeModifyRecord($li_results); } sub insertMech{ my ($mech_results ) = @_; my $mech_array_values = SomethingUnPipeModifyRecord($mech_results);
      LI_ID can have multiple action_type or TAGET

      Are the multiples always the same ?
      For example, if there are 3 action_type values, will there always be 3 TAGET values ?

      poj
        More important: does it matter which TARGET value comes in which position?
        No. It can have be any number of values. Sometimes they might not have any value at all. Could be a null array.
        No . It doesn't matter on the index of the array
Re^2: access array of values without a loop
by spie287 (Novice) on Feb 20, 2015 at 16:57 UTC
    Hi All, Thanks for all your comments. I think I have more questions than I initially had. Which is good ! Now I understand the XY problem. I have a database which has 12 tables. The keys are column names from the excel sheet. This is my primary table and the rest in one to many relationship with this table. I have one value for the primary column in my LI table(COLUMN NAME LI) and can have multiple values for different columns in the same or different table. my $li_insert = "insert into LI values(LI_SEQ.nextval,?,?,?,?,?,?,?,?,?,sysdate,?,?,?,?)"; my $mechanism_insert = "insert into mechanism (TAGET,ACTIONTYPE,ID,LI_ID) values(?,?,MECH_SEQ.nextval,LI_SEQ.currval)"; Here one LI_ID can have multiple action_type or TAGET. So I want my script to be able to insert 5,2,1,12 6,1,2,12. The data would be like this: { 'taget '=> 5,6, 'Action_type' =>2,1 } in the excel sheet it actually comes as joined with a pipe operator I split them to have an array in a subroutine. How could I achieve this type of insertion if I keep them joined? Hope this is clear now!Let me know if you need any more details.
    # to get each hash from an array of hash foreach my $results(@$hash_of_excel){ my $specificresults = GroupResults($results); } sub GroupResults{ #to getonly the relevant columns(excel) for each table in the database +. #%details is a global hash tha holds different tables and their corres +ponding column my( $AllRecords )= @_; my %specificResults; for my $info (keys %details){ $specificResults{$info}{$_} = $AllRecords->{$_} for @{$details{$in +fo}; } } return \%specificResults; } sub SomethingUnPipeModifyRecord { #convert the joined values to array of values my( $rec ) = @_; for my $val( values %$rec ){ my $newval = [ split /\|/, $val ]; $val = $newval; } return $rec; } sub insertLI{ my ($li_results )= @_; my $li_array_values = SomethingUnPipeModifyRecord($li_results); } sub insertMech{ my ($mech_results ) = @_; my $mech_array_values = SomethingUnPipeModifyRecord($mech_results);