Sponsored Content
Special Forums UNIX and Linux Applications SQL database call into Multidimensional Array using Perl Script Post 302586230 by durden_tyler on Friday 30th of December 2011 06:10:38 PM
Old 12-30-2011
You could do something like this -

Code:
C:\>
C:\>type fetchdata.pl
#!perl -w
use strict;
use DBI;
 
my @all_data;
my $sql = "SELECT empno, ename FROM emp";
my $dbh = DBI->connect ("dbi:Oracle:db_name", "user", "password");
my $sth = $dbh->prepare ($sql);
$sth->execute();
while (my @row = $sth->fetchrow()) {
 push @all_data, [@row];
}
$sth->finish();
$dbh->disconnect();
 
# ##############################################################################
# The array @all_data looks like this now.
# Each array element is a reference to an anonymous array that has the "Empno"
# and "Ename" values.
# ##############################################################################
#   $all_data[1]  = [ "7369", "SMITH"  ];
#   $all_data[2]  = [ "7499", "ALLEN"  ];
#   $all_data[3]  = [ "7521", "WARD"   ];
#   $all_data[4]  = [ "7566", "JONES"  ];
#   $all_data[5]  = [ "7654", "MARTIN" ];
#   $all_data[6]  = [ "7698", "BLAKE"  ];
#   $all_data[7]  = [ "7782", "CLARK"  ];
#   $all_data[8]  = [ "7788", "SCOTT"  ];
#   $all_data[9]  = [ "7839", "KING"   ];
#   $all_data[10] = [ "7844", "TURNER" ];
#   $all_data[11] = [ "7876", "ADAMS"  ];
#   $all_data[12] = [ "7900", "JAMES"  ];
#   $all_data[13] = [ "7902", "FORD"   ];
#   $all_data[14] = [ "7934", "MILLER" ];
# ##############################################################################
 
# loop and print
foreach my $item (@all_data) {
 printf ("Empno = %4d  Ename = %-s\n", @$item );
}
 
C:\>
C:\>perl fetchdata.pl
Empno = 7369  Ename = SMITH
Empno = 7499  Ename = ALLEN
Empno = 7521  Ename = WARD
Empno = 7566  Ename = JONES
Empno = 7654  Ename = MARTIN
Empno = 7698  Ename = BLAKE
Empno = 7782  Ename = CLARK
Empno = 7788  Ename = SCOTT
Empno = 7839  Ename = KING
Empno = 7844  Ename = TURNER
Empno = 7876  Ename = ADAMS
Empno = 7900  Ename = JAMES
Empno = 7902  Ename = FORD
Empno = 7934  Ename = MILLER
 
C:\>
C:\>

Or you could also use selectall_arrayref() to fetch all data at once, store it in a multidimensional array and return the reference of that array.

Code:
C:\>
C:\>type fetchdata_1.pl
#!perl -w
use strict;
use DBI;
 
my $sql = "SELECT empno, ename FROM emp";
my $dbh = DBI->connect ("dbi:Oracle:db_name", "user", "password");
my $all_data = $dbh->selectall_arrayref($sql);
$dbh->disconnect();
 
# ##############################################################################
# The scalar variable $all_data is a reference to an anonymous array. Let's say
# that array is @x. Then:
#   $all_data = [ @x ];
# And the elements of array @x are as follows:
# ##############################################################################
#   $x[1]  = [ "7369", "SMITH"  ];
#   $x[2]  = [ "7499", "ALLEN"  ];
#   $x[3]  = [ "7521", "WARD"   ];
#   $x[4]  = [ "7566", "JONES"  ];
#   $x[5]  = [ "7654", "MARTIN" ];
#   $x[6]  = [ "7698", "BLAKE"  ];
#   $x[7]  = [ "7782", "CLARK"  ];
#   $x[8]  = [ "7788", "SCOTT"  ];
#   $x[9]  = [ "7839", "KING"   ];
#   $x[10] = [ "7844", "TURNER" ];
#   $x[11] = [ "7876", "ADAMS"  ];
#   $x[12] = [ "7900", "JAMES"  ];
#   $x[13] = [ "7902", "FORD"   ];
#   $x[14] = [ "7934", "MILLER" ];
# ##############################################################################
# i.e. each array element is a reference to an anonymous array that has the
# "Empno" and "Ename" values.
 
# loop and print the data
foreach my $item (@$all_data) {
 printf ("Empno = %4d  Ename = %-s\n", @$item );
}
 
C:\>
C:\>perl fetchdata_1.pl
Empno = 7369  Ename = SMITH
Empno = 7499  Ename = ALLEN
Empno = 7521  Ename = WARD
Empno = 7566  Ename = JONES
Empno = 7654  Ename = MARTIN
Empno = 7698  Ename = BLAKE
Empno = 7782  Ename = CLARK
Empno = 7788  Ename = SCOTT
Empno = 7839  Ename = KING
Empno = 7844  Ename = TURNER
Empno = 7876  Ename = ADAMS
Empno = 7900  Ename = JAMES
Empno = 7902  Ename = FORD
Empno = 7934  Ename = MILLER
 
C:\>
C:\>

Hope that helps,
tyler_durden
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

multidimensional array in perl

i'm trying to open a file with three or more columns and an undetermined, but finite number of rows. I want to define an array for each row with each element of the row as a sub array. The columns are separated by tabs or spaces. Here's the file: 12x3.12z34b.342sd3.sds 454.23.23.232 ... (9 Replies)
Discussion started by: prkfriryce
9 Replies

2. Shell Programming and Scripting

Awk multidimensional Array

Hello Experts,, Can anybody give me a brief idea what is following bold letter statement is for!! what is the term called so that I can google for it.. It seems to be an array inside another array.. awk' /TXADDR/ { txaddr=$NF } ##understood /TXDATA/ { txdata]=$NF... (1 Reply)
Discussion started by: user_prady
1 Replies

3. Shell Programming and Scripting

AWK multidimensional array

In a single dim. awk array, we can use : <index> in <array name> to determine whether a particualar index exists in the array or not. Is there a way to achieve this in a awk multi dim. array ? (4 Replies)
Discussion started by: sinpeak
4 Replies

4. Programming

multidimensional array using c++ vector

Hi! I need to make dynamic multidimensional arrays using the vector class. I found in this page How to dynamically create a two dimensional array? - Microsoft: Visual C++ FAQ - Tek-Tips the way to do it in 2D, and now i'm trying to expand it to 3D but i don't understand how is the operator working,... (0 Replies)
Discussion started by: carl.alv
0 Replies

5. Shell Programming and Scripting

Perl help: Creating a multidimensional array of subdirectories and its contents

I'm currently working with dozens of FASTA files, and I'm tired of having to manually change the filename in my Perl script. I'm trying to write a simple Perl script that'll create a 2-dimensional array containing the name of the folders and its contents. For example, I would like the output... (6 Replies)
Discussion started by: shwang3
6 Replies

6. Shell Programming and Scripting

perl-data from file save to multidimensional array

i have a file,like 1 3 4 5 6 7 8 9 i want to save it into an array. and then i want to get every element, because i want to use them to calculate. for example: i want to calculate 1 + 3. but i cannot reach my goal. open (FILE, "<", "number"); my @arr; while (<FILE>){ chomp;... (1 Reply)
Discussion started by: pp-zz
1 Replies

7. Shell Programming and Scripting

multidimensional array in awk

Hi, I was trying to process a file with the help of awk. I want to first display all the rows that contains 01 and at the end of processing I have to print some portion of all the lines. like below. Output expected: (2 Replies)
Discussion started by: ahmedwaseem2000
2 Replies

8. Shell Programming and Scripting

PERL : Bind 2D array to SQL

Hi, I am in the need of doing a bulk insert via : SQL - INSERT INTO <table> (SELECT..) OR PLSQL Block - FORALL i IN 1 .. count INSERT INTO <table>(arrayname(i)) I have a 2D array in my perl code which has the rows to be bulk inserted. Is there a way to bind the 2D array to the SQL... (4 Replies)
Discussion started by: sinpeak
4 Replies

9. Shell Programming and Scripting

Pass perl array to SQL oracle

Hello, Could someone please suggest if there is a way to pass an perl array(pass @v_array) to SQL as in below eg : #!/usr/bin/perl @v_array = (1,2,4,5,6,8); $db_userid = 'ni71/ni711'; $bufTPO = qx{ sqlplus -s << EOF $db_userid set verify off set feedback off set... (1 Reply)
Discussion started by: arunshankar.c
1 Replies

10. Shell Programming and Scripting

Multidimensional array

I am learning about bash system variables, such as $ , @ and #. I have this piece of script implementing an array and it is doing its job just fine. This is not the only array I will be using. Just for ease of maintenance and more coding I would like to have the arrays in two dimensional... (4 Replies)
Discussion started by: annacreek
4 Replies
All times are GMT -4. The time now is 03:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy