perl functions and arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl functions and arrays
# 1  
Old 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?
# 2  
Old 01-17-2008
this is the code i wrote before...

but this is an hard coded one...
Instead of hard coding i wanna the function to fetch that for each record of the id field and store it.

[code = Perl]
#!/usr/bin/perl
use DBI;

$var_id = "3";
sub read_db
{


$dbh = DBI -> connect('DBI:Oracle:mercury','student','learn') || die "unable to connect:$DBI::errstr";


my $sql = qq/SELECT id, passwd, name FROM rules_test where id = '$var_id '/;
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) {
print "$col1, $col2, $col3\n";
#print "\n\n\n$col1\n";

}
}
&read_quality_db($var_rule);

[/code]


thanks

mercury
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help figuring out how to create sharable functions in Perl

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

2. Programming

Perl: How to load some functions automaticaly, starting Perl inteructively (with -d -e 0)?

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

3. Shell Programming and Scripting

Arrays and functions

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

4. Shell Programming and Scripting

Arrays in perl

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

5. Shell Programming and Scripting

Perl script to find where functions is called in c

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

6. Shell Programming and Scripting

perl - passing hash references to functions

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

7. Shell Programming and Scripting

perl arrays

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

8. Shell Programming and Scripting

Passing arrays between functions

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

9. Shell Programming and Scripting

Perl - Compare 2 Arrays

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

10. Programming

perl arrays

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
Login or Register to Ask a Question