?
Solved

passing an argument to a subroutine

Posted on 2004-03-18
4
Medium Priority
?
526 Views
Last Modified: 2013-12-25
Hey guys quick help with some code if you could.  Trying to pass an element of an array to a subroutine and having some troubles.  can someone clean up my code.  Also sorry for the way its written, im used to programming in Java or C++ so I comment everything.  Also Id preffer it to be a refference passed even though the value will never change, id like to learn how to refference an array element as apposed to just storing its value in another variable and passing the new variable.

#Windows-based Perl/DBI/MS Access example

use DBI;

&openHTMLTags();                                  #Merely opens HTML Tags
&connectToDatabase();                            #Self-explanatory
@theTables = &tableNames();                  #Works fine gets me a list of table names into an array.
&openTableHeadings();                            #Prints HTML code for a table and its headings

foreach $nameOfTable(@theTables)
{
   print "$nameOfTable";                          #prints out the table name just fine to make sure it works.
   &getFields($nameOfTable);                   #PROBLEMS HERE!!!!
   &printTableRecords();
}
&closingHTMLTags();                               #Self-explanatory.



#// Pre:  none
#// Post: Sql statments required to retrieve fields from database
sub getFields($dataTableName)
{
   print "$dataTableName";              #####WONT PRINT HERE!
   #construct SQL statement
   $sqlstatement="SELECT ID,FirstName,LastName,Company,Email,Location,Industry,Phone,Fax FROM $dataTableName";

   #prepare and execute SQL statement
   $sth = $dbh->prepare($sqlstatement);
   $sth->execute ||
         die "Could not execute SQL statement, maybe invalid?";

}  # end getFields()

0
Comment
Question by:NoodlesWIU
  • 2
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 2000 total points
ID: 10630037
use DBI;

&openHTMLTags();                                  #Merely opens HTML Tags
&connectToDatabase();                            #Self-explanatory
@theTables = &tableNames();                  #Works fine gets me a list of table names into an array.
&openTableHeadings();                            #Prints HTML code for a table and its headings

foreach $nameOfTable(@theTables)
{
  print "$nameOfTable";                          #prints out the table name just fine to make sure it works.
  &getFields($nameOfTable);                   #PROBLEMS HERE!!!!
  &printTableRecords();
}
&closingHTMLTags();                               #Self-explanatory.



#// Pre:  none
#// Post: Sql statments required to retrieve fields from database
sub getFields {
  my $dataTableName = shift;

  print "$dataTableName";              #####WILL PRINT HERE!
  #construct SQL statement
  $sqlstatement="SELECT ID,FirstName,LastName,Company,Email,Location,Industry,Phone,Fax FROM $dataTableName";

  #prepare and execute SQL statement
  $sth = $dbh->prepare($sqlstatement);
  $sth->execute ||
         die "Could not execute SQL statement, maybe invalid?";

}  # end getFields()

No need for a reference, as $dataTableName is local to the subroutine.
0
 
LVL 3

Author Comment

by:NoodlesWIU
ID: 10630586
Wow, thats all it took.  Thank you very much, but question.  what does "shift" do.  Ive never seen that before.  Thank you again for the help
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10631942
shift, shifts the first value off the array and returns it.

It could also been written as:

my $dataTableName = @_;
0
 
LVL 3

Author Comment

by:NoodlesWIU
ID: 10636097
lol  Thats funny I was so close before I had done it like you did except I put my $dataTableName = $_;


one character off.  Well thank you, much appreciated.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question