Sponsored Content
Special Forums UNIX and Linux Applications SQL database call into Multidimensional Array using Perl Script Post 302585952 by eazyeddie22 on Thursday 29th of December 2011 06:16:40 PM
Old 12-29-2011
Java SQL database call into Multidimensional Array using Perl Script

Code:
#!/usr/local/bin/perl
use DBI;
use File::Copy;
use Time::Local;
use Data::Dumper;

-Comments Describing what I'm doing--------------
-I'm pulling information from a database that has an ID and Name. They are separated by a space and I'm trying to load them into a multidimensional array so as to manipulate Name of each element while keeping it's relationship to the ID constant.--------------------------

When I print #1, the information displays exactly how it should, but somewhere between the push and split function it goes wrong.

Code:
 $stm3 = $dbh->prepare($profile);
        $stm3->execute();
#       while (@row = $stm3->fetchrow_array)
        while (@row = $stm3->fetchrow()) {
#1               print "This is the Array: $row[0] $row[1]\n";
                @tmp = ([split / /, @row]);
                print "tmp is 0: $tmp[0] \n ";
#               push (@final, @tmp);
        #       print "E1: $final[0] E2: $final[1]\n";
        }

-I'm using the following statement to print out the results of the multi-dimensional array--------------------

Code:
for($i=0; $i<=$#final; $i++)
{
  print "i=$i\n";
  for($j=0; $j<2; $j++)
  {
    print " j = $j, Array: $final[$i][$j]";
  }
  print "\n";
}

Any thing will help.

Last edited by radoulov; 12-30-2011 at 04:50 PM.. Reason: Code tags!
 

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
PDOSTATEMENT.FETCH(3)							 1						     PDOSTATEMENT.FETCH(3)

PDOStatement::fetch - Fetches the next row from a result set

SYNOPSIS
public mixed PDOStatement::fetch ([int $fetch_style], [int $cursor_orientation = PDO::FETCH_ORI_NEXT], [int $cursor_offset]) DESCRIPTION
Fetches a row from a result set associated with a PDOStatement object. The $fetch_style parameter determines how PDO returns the row. PARAMETERS
o $fetch_style - Controls how the next row will be returned to the caller. This value must be one of the PDO::FETCH_* constants, defaulting to value of PDO::ATTR_DEFAULT_FETCH_MODE (which defaults to PDO::FETCH_BOTH). o PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set o PDO::FETCH_BOTH (default): returns an array indexed by both column name and 0-indexed column number as returned in your result set o PDO::FETCH_BOUND: returns TRUE and assigns the values of the columns in your result set to the PHP variables to which they were bound with the PDOStatement.bindColumn(3) method o PDO::FETCH_CLASS: returns a new instance of the requested class, mapping the columns of the result set to named properties in the class. If $fetch_style includes PDO::FETCH_CLASSTYPE (e.g. PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE) then the name of the class is determined from a value of the first column. o PDO::FETCH_INTO: updates an existing instance of the requested class, mapping the columns of the result set to named prop- erties in the class o PDO::FETCH_LAZY: combines PDO::FETCH_BOTH and PDO::FETCH_OBJ, creating the object variable names as they are accessed o PDO::FETCH_NAMED: returns an array with the same form as PDO::FETCH_ASSOC, except that if there are multiple columns with the same name, the value referred to by that key will be an array of all the values in the row that had that column name o PDO::FETCH_NUM: returns an array indexed by column number as returned in your result set, starting at column 0 o PDO::FETCH_OBJ: returns an anonymous object with property names that correspond to the column names returned in your result set o $cursor_orientation - For a PDOStatement object representing a scrollable cursor, this value determines which row will be returned to the caller. This value must be one of the PDO::FETCH_ORI_* constants, defaulting to PDO::FETCH_ORI_NEXT. To request a scrollable cursor for your PDOStatement object, you must set the PDO::ATTR_CURSOR attribute to PDO::CURSOR_SCROLL when you prepare the SQL statement with PDO.prepare(3). o $offset - For a PDOStatement object representing a scrollable cursor for which the cursor_orientation parameter is set to PDO::FETCH_ORI_ABS, this value specifies the absolute number of the row in the result set that shall be fetched. For a PDOState- ment object representing a scrollable cursor for which the cursor_orientation parameter is set to PDO::FETCH_ORI_REL, this value specifies the row to fetch relative to the cursor position before PDOStatement.fetch(3) was called. RETURN VALUES
The return value of this function on success depends on the fetch type. In all cases, FALSE is returned on failure. EXAMPLES
Example #1 Fetching rows using different fetch styles <?php $sth = $dbh->prepare("SELECT name, colour FROM fruit"); $sth->execute(); /* Exercise PDOStatement::fetch styles */ print("PDO::FETCH_ASSOC: "); print("Return next row as an array indexed by column name "); $result = $sth->fetch(PDO::FETCH_ASSOC); print_r($result); print(" "); print("PDO::FETCH_BOTH: "); print("Return next row as an array indexed by both column name and number "); $result = $sth->fetch(PDO::FETCH_BOTH); print_r($result); print(" "); print("PDO::FETCH_LAZY: "); print("Return next row as an anonymous object with column names as properties "); $result = $sth->fetch(PDO::FETCH_LAZY); print_r($result); print(" "); print("PDO::FETCH_OBJ: "); print("Return next row as an anonymous object with column names as properties "); $result = $sth->fetch(PDO::FETCH_OBJ); print $result->name; print(" "); ?> The above example will output: PDO::FETCH_ASSOC: Return next row as an array indexed by column name Array ( [name] => apple [colour] => red ) PDO::FETCH_BOTH: Return next row as an array indexed by both column name and number Array ( [name] => banana [0] => banana [colour] => yellow [1] => yellow ) PDO::FETCH_LAZY: Return next row as an anonymous object with column names as properties PDORow Object ( [name] => orange [colour] => orange ) PDO::FETCH_OBJ: Return next row as an anonymous object with column names as properties kiwi Example #2 Fetching rows with a scrollable cursor <?php function readDataForwards($dbh) { $sql = 'SELECT hand, won, bet FROM mynumbers ORDER BY BET'; try { $stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL)); $stmt->execute(); while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) { $data = $row[0] . " " . $row[1] . " " . $row[2] . " "; print $data; } $stmt = null; } catch (PDOException $e) { print $e->getMessage(); } } function readDataBackwards($dbh) { $sql = 'SELECT hand, won, bet FROM mynumbers ORDER BY bet'; try { $stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL)); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST); do { $data = $row[0] . " " . $row[1] . " " . $row[2] . " "; print $data; } while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_PRIOR)); $stmt = null; } catch (PDOException $e) { print $e->getMessage(); } } print "Reading forwards: "; readDataForwards($conn); print "Reading backwards: "; readDataBackwards($conn); ?> The above example will output: Reading forwards: 21 10 5 16 0 5 19 20 10 Reading backwards: 19 20 10 16 0 5 21 10 5 SEE ALSO
PDO.prepare(3), PDOStatement.execute(3), PDOStatement.fetchAll(3), PDOStatement.fetchColumn(3), PDOStatement.fetchObject(3), PDOState- ment.setFetchMode(3). PHP Documentation Group PDOSTATEMENT.FETCH(3)
All times are GMT -4. The time now is 05:15 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy