Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mental block: Passing array of arrays to sub

Status
Not open for further replies.

aswolff

Programmer
Jul 31, 2006
100
US
I have an array of arrays (3x6) and I pass it to a subroutine to loop thru and insert into a table. But when I pass the array to the sub the array seems to become a (1x6).

Code:
@rowarr =    ( [ "ROLE", "ROLE TEST1", "HRSiteAdmin", "HR1", "Admin", 0 ],
               [ "ROLE", "ROLE TEST2", "PayrollSup", "HR1", "Supervisor", 0 ],
               [ "ROLE", "ROLE TEST3", "HRCorpExec", "HR1", "Executive", 0 ]
             );

my $qry = qq{ INSERT INTO LAWIXREFNB VALUES ( ?, ?, ?, ?, ?, ? ) };

query_ora($qry, @rowarr);

sub query_ora
{
  my $qry = shift;
  my @records = shift;
  $olen = @records;
  print "length outer array : $olen\n";
  foreach (@records)
    {
    ....
    ...
    }

The end result of the above is that only the first array gets processed (i.e. ["ROLE", "ROLE TEST1", "HRSiteAdmin", "HR1", "Admin", 0 ]

I guess I do not understand the rules when it comes to passing arrays to subs. Any help is appreciated.
 
I am happy to announce that I figured out my problem...

Code:
@rowarr =    ( [ "ROLE", "ROLE TEST1", "HRSiteAdmin", "HR1", "Admin", 0 ],
               [ "ROLE", "ROLE TEST2", "PayrollSup", "HR1", "Supervisor", 0 ],
               [ "ROLE", "ROLE TEST3", "HRCorpExec", "HR1", "Executive", 0 ]
             );

my $qry = qq{ INSERT INTO LAWIXREFNB VALUES ( ?, ?, ?, ?, ?, ? ) };

query_ora($qry, @rowarr);

sub query_ora
{
  my ($qry, @records) = @_;      ###<---- Params into a sub are passed as an array as well.  My previous shift was only passing the first 2 arguments and not all 4.
  $olen = @records;
  print "length outer array : $olen\n";
  foreach (@records)
    {
    ....
    ...
    }
 
arrays passed to a sub are not really passed as an array, they are passed as a flattened list. This has lots of implications for how you pass arrays or hashes to subs. What you really should do is a pass a reference to the array to the sub:

Code:
@rowarr =    ( [ "ROLE", "ROLE TEST1", "HRSiteAdmin", "HR1", "Admin", 0 ],
               [ "ROLE", "ROLE TEST2", "PayrollSup", "HR1", "Supervisor", 0 ],
               [ "ROLE", "ROLE TEST3", "HRCorpExec", "HR1", "Executive", 0 ]
             );

my $qry = qq{ INSERT INTO LAWIXREFNB VALUES ( ?, ?, ?, ?, ?, ? ) };

query_ora($qry, \@rowarr);# the \ creates a reference to the array

sub query_ora
{
  my $qry = shift;
  my $records = shift;
  my @records = @{$records}; #turn it back into an array 
  $olen = @records;
  print "length outer array : $olen\n";
  foreach (@records)
    {
    ....
    ...
    }

You way works for one scalar and one array, but try passing two arrays that way and you will see where the problem is. Make double sure to use "strict" and "warnings" in all your perl programs.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Actually when I try to do what you suggest I get error:

Code:
Can't use an undefined value as an ARRAY reference at ./temproledmp.pl line 71,
<DATA> line 228.

Code:
query_ora($qry, \@rowarr);

sub query_ora
{
    use DBI qw(:sql_types);
    my $qry = shift;
    my $arr_ref = shift;
    my @records = @{$arr_ref};  <===LINE 71
.
.
.
 
$qry or @rowarr have not been defined when sent to the subroutine. Here is an example that shows the syntax is proper:

Code:
my $qry = 'test';
my @rowarr = (1..10);
query_ora($qry, \@rowarr);

sub query_ora
{
    my $qry = shift;
    my $arr_ref = shift;
    my @records = @{$arr_ref};
    print "\$qry = $qry\n";
    print "$_\n" for @records;
}

output:

$qry = test
1
2
3
4
5
6
7
8
9
10


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top