01-17-2008
perl functions and arrays
Hi,
First I will tell my objective of this function (function one).
I have a table for ex:
id passwd name
-- ------ -----
1 fdhgfs werwer
2 fsdfs sdfsdf
3 sdfs sdfsdf
4 fdsfs dssdf
. . .
.
.
.
The id, passwd and name are the arguments for another function say two.
The function one should store the values of the table and it should be passed as arguments to function two, one row at a time.
The both functions must be independent also.
code:
#!/usr/bin/perl
use DBI;
my $var_id;
sub read_db
{
$dbh = DBI -> connect('DBI:Oracle:mercury','student','learn') || die "unable to connect:$DBI::errstr";
#Here I am counting the number rows the table has, so that i can use that for the limit in the for loop
my $count = qq/select count(id) from rules_test/;
my $sth = $dbh->prepare($count);
$sth->execute();
print "$count";
#Here I am getting values of id
my $var_id = qq/SELECT id FROM rules_test/;
my $sth = $dbh->prepare($var_id);
$sth->execute();
while(@data = $sth-> fetchrow_array())
{
my $var_id = data[1];
for ($i=1; $i=$count; $i++)
{
my $sql = qq/SELECT id, passwd, name FROM rules_test where id = data[i]/;
my $sth = $dbh->prepare($sql);
$sth->execute();
my ($col1, $col2, $col3);
$sth->bind_col(1, \$col1);
$sth->bind_col(2, \$col2);
$sth->bind_col(3, \$col3);
while ($sth->fetch) {
$sth->bind_col(3, \$col3);
while ($sth->fetch) {
print "$col1, $col2, $col3\n";
}
}
}
&read_db($var_id);
}
-------code ends--------------
I think I am lost somewhere.....
Could u guys help me in this?
10 More Discussions You Might Find Interesting
1. Programming
hello ppl,
i'm coding a perl script and i have the following situation:
@array1 = ("test1", "test2", "test3");
@array2 = ("something1", "something2", "something1");
$var1 = "with_one_of_the_array1_values";
$var2 = "with_one_of_the_array2_values";
what i want to do is to compare $var1... (2 Replies)
Discussion started by: crashnburn
2 Replies
2. Shell Programming and Scripting
Hi all,
I have the following script where the contents of file1 and file2 would be something like this:
file1:
56790,0,0,100998765
89756,0,0,100567876
867645,1,3,678777654
file2:
56790,0,0,100998765
65776,0,0,4766457890
+5896,0,0,675489876
What I then want to do is check if... (4 Replies)
Discussion started by: Donkey25
4 Replies
3. Shell Programming and Scripting
Hi,
I have a function that hold 3 arrayies.
I need to pass them to another function as an input, for further use
Could you please explain how to do that.
Thanks (5 Replies)
Discussion started by: yoavbe
5 Replies
4. Shell Programming and Scripting
Hi
I need some help using arrays in perl.
I have an array say var and a variable var1.
I want to check if the var1 is present in the array. How do I check that ?
my @var = 1...10;
my $var1 =5;
if ( $var1 in @var )
{
.......
}
else
{
.......
}
Something like above. Can some... (2 Replies)
Discussion started by: ammu
2 Replies
5. Shell Programming and Scripting
hi there
I have the following script in which i have created a PrintHash() function.
I want to pass to this function the reference to a hash (in the final code i will be passing different hashes to this print function hence the need for a function). I am getting an error
Type of arg 1 to... (1 Reply)
Discussion started by: hcclnoodles
1 Replies
6. Shell Programming and Scripting
Hello,
I need to write a perl script to find where functions is called in c files..
The script should scan the file and find out the function names and then search to see where they are called...
Lets for now assume all functions are internal.
I don't know where to start :(
... (4 Replies)
Discussion started by: bojomojo
4 Replies
7. Shell Programming and Scripting
Hi all,
I have a log file which has logs. I am reading logs line by line into perl arrays. I want to print all the arrays elements starting from 8(word) to end of the line.
print array......array to a new file. and I have to do it in perl as res of the program in perl.
Please help me on... (9 Replies)
Discussion started by: firestar
9 Replies
8. Shell Programming and Scripting
Hi Guys! I need to solve this.
I want an array to be created by a certain calculation for which I created a function. Now this array is not getting created. See script below I want array b to be the factorial value of array element a. Help is needed. Thanks!
#!/bin/bash
echo "Number of... (17 Replies)
Discussion started by: ambijat
17 Replies
9. Programming
I would like to use Perl in 'interactive' mode (kind off), starting it by
> perl -d -e 0;
But I need to have some function be read on loading.
Also, it should not be for anyone who starting Perl or use it any how.
I did try to search, but not much result.
I have try a file '.perldb':... (1 Reply)
Discussion started by: alex_5161
1 Replies
10. Shell Programming and Scripting
I am somewhat new to Perl. I have Googled Perl one liners and worked with the MIME::Lite library to send emails with attachments. But I have not done any real Perl scripting. I need to write a script to install code for our application using an Oracle database with DBI, and to track in the... (4 Replies)
Discussion started by: gandolf989
4 Replies
LEARN ABOUT DEBIAN
dbix::contextualfetch
DBIx::ContextualFetch(3pm) User Contributed Perl Documentation DBIx::ContextualFetch(3pm)
NAME
DBIx::ContextualFetch - Add contextual fetches to DBI
SYNOPSIS
my $dbh = DBI->connect(...., { RootClass => "DBIx::ContextualFetch" });
# Modified statement handle methods.
my $rv = $sth->execute;
my $rv = $sth->execute(@bind_values);
my $rv = $sth->execute(@bind_values, @bind_cols);
# In addition to the normal DBI sth methods...
my $row_ref = $sth->fetch;
my @row = $sth->fetch;
my $row_ref = $sth->fetch_hash;
my %row = $sth->fetch_hash;
my $rows_ref = $sth->fetchall;
my @rows = $sth->fetchall;
my $rows_ref = $sth->fetchall_hash;
my @tbl = $sth->fetchall_hash;
DESCRIPTION
It always struck me odd that DBI didn't take much advantage of Perl's context sensitivity. DBIx::ContextualFetch redefines some of the
various fetch methods to fix this oversight. It also adds a few new methods for convenience (though not necessarily efficiency).
SET-UP
my $dbh = DBIx::ContextualFetch->connect(@info);
my $dbh = DBI->connect(@info, { RootClass => "DBIx::ContextualFetch" });
To use this method, you can either make sure that everywhere you normall call DBI->connect() you either call it on DBIx::ContextualFetch,
or that you pass this as your RootClass. After this DBI will Do The Right Thing and pass all its calls through us.
EXTENSIONS
execute
$rv = $sth->execute;
$rv = $sth->execute(@bind_values);
$rv = $sth->execute(@bind_values, @bind_cols);
execute() is enhanced slightly:
If called with no arguments, or with a simple list, execute() operates normally. When when called with two array references, it performs
the functions of bind_param, execute and bind_columns similar to the following:
$sth->execute(@bind_values);
$sth->bind_columns(undef, @bind_cols);
In addition, execute will accept tainted @bind_values. I can't think of what a malicious user could do with a tainted bind value (in the
general case. Your application may vary.)
Thus a typical idiom would be:
$sth->execute([$this, $that], [($foo, $bar)]);
Of course, this method provides no way of passing bind attributes through to bind_param or bind_columns. If that is necessary, then you
must perform the bind_param, execute, bind_col sequence yourself.
fetch
$row_ref = $sth->fetch;
@row = $sth->fetch;
A context sensitive version of fetch(). When in scalar context, it will act as fetchrow_arrayref. In list context it will use
fetchrow_array.
fetch_hash
$row_ref = $sth->fetch_hash;
%row = $sth->fetch_hash;
A modification on fetchrow_hashref. When in scalar context, it acts just as fetchrow_hashref() does. In list context it returns the
complete hash.
fetchall
$rows_ref = $sth->fetchall;
@rows = $sth->fetchall;
A modification on fetchall_arrayref. In scalar context it acts as fetchall_arrayref. In list it returns an array of references to rows
fetched.
fetchall_hash
$rows_ref = $sth->fetchall_hash;
@rows = $sth->fetchall_hash;
A mating of fetchall_arrayref() with fetchrow_hashref(). It gets all rows from the hash, each as hash references. In scalar context it
returns a reference to an array of hash references. In list context it returns a list of hash references.
ORIGINAL AUTHOR
Michael G Schwern as part of Ima::DBI
CURRENT MAINTAINER
Tony Bowden <tony@tmtm.com>
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SEE ALSO
DBI. Ima::DBI. Class::DBI.
perl v5.10.0 2005-09-27 DBIx::ContextualFetch(3pm)