Solved

Setting all fields in a hash to name/value from sql...

Posted on 2004-09-13
1
184 Views
Last Modified: 2013-12-25
Hi there,

I am just moving code from one server to another, where my current code uses the following syntax to get all the variables returning from the sql output (saves defining each time)....

                while( $hash =  $sth->fetchrow_hashref() )
                          {
                                           foreach( keys %$hash ){
                             $$_ = $hash->{$_}
                                 }

However now the server uses the folowing syntax, and I am not sure how to convert the statement above to "catch all" the output ......

      while (%hash = $sth->fetchhash)
      {
            $pagetemplate=$hash{'pagetemplate'};
            $fulltitledisplay=$hash{'fulltitle'};
            $pageviews=$hash{'pageviews'};
      }

can someone help me out please!
0
Comment
Question by:dreamshockDesign
1 Comment
 
LVL 14

Accepted Solution

by:
tomaugerdotcom earned 50 total points
ID: 12044500
You're looking for:

while (%hash = $sth->fetchhash) {
  foreach (keys %hash) {
    $$_ = $hash{$_};
  }
}

The only difference here is that you're working with a hash and not a hash reference as in your first example.

Note that neither this nor your code above will work under strict, because you're creating variable variables on-the-fly as it were.

Hope this helps,

Tom
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

830 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