Re: dereferencing issues

Thomas Nyman <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.html-template
Message-ID <[email protected]>
Thanks..

I managed to figure it out but yor mail was very instructional.

What I did was as follows
..some code...

$rows = $sth->fetchall_arrayref({});

my @AoH = @$rows;
for my $p (0..$#AoH) {

                         for my $bulle ( keys %{ $AoH[$p] } ) {

                                                                 if  
(($AoH[$p]{$bulle}) =~ m/^[\d]+-[\d]+$/) {


                                                                         
                  my @fields = split /-/, ($AoH[$p]{$bulle});
                                                                         
                  my $nr1 = "-";
                                                                         
                  my $nr2 = "-";
                                                                         
                  $rows->[$p]->{Mobil} = $nr1.$fields[0].$fields[1]. 
$nr2;
                                                                         
                  }
        }
}


Its probably not elegant but it gives me a chance to change the value  
of row "Mobil" before feeding it to the template. In this particular  
example it doesn do anything useful I was simply doing it to learn.

Your input was greatly appreciated and if you see me missing  
something obvious the give me a "holler". And thanks for the dumper  
idea..did really know about that.

Thomas


12 aug 2005 kl. 16.06 skrev Ragan, Steve:

>
> To understand how to de-ref you must understand what it is you're  
> trying to de-ref.
>
> Lets assume you have a simple SQL query like this that returns  
> first and last names from a table -
>
>     my $sql = qq[
>     SELECT first, last
>       FROM persons
>     ];
>
> First you prepare and execute -
>
>     my $sth = $dbh->prepare($sql) || die("Unable to prepare  
> statement");
>
>     $sth->execute() || die("Database execute Error: $DBI::errstr");
>
> then fetch the results. If you use the regular fetchall_arrayref(),  
> you're getting an arrarref of arrayref structure.
> That is a reference to array of rows that itself is a reference to  
> array of columns.
> To help visualize the structure you can use the Data::Dumper module
>
>     use Data::Dumper;
>
>     my $ra_results = $sth->fetchall_arrayref();
>
>     my $d = Data::Dumper->new([$ra_results], [qw(ra_results )] );
>
>
>     print '<pre>' . $d->Dump . '</pre>';   # if you in HTML,  
> otherwise just print $d->Dump
>
>
> The structure would appear something like this -
>
> $ra_results = [
>                 [
>                   'Duane',
>                   'Allman'
>                 ],
>                 [
>                   'Stevie Ray',
>                   'Vaughn'
>                 ]
>               ];
>
>
> You could access specific rows and columns directly -
>
>   First row -
>
>     my $first_name = $ra_results->[0][0];
>     my $last_name  = $ra_results->[0][1];
>
> or loop through all rows accessing each column -
>
>     for my $ra_row (@{$ra_results}) {
>         my $first_name = $ra_row->[0];
>           my $last_name  = $ra_row->[1];
>           ...( more code here ) ...
>     }
>
>
>
>
> If you fetch using $sth->fetchall_arrayref({}), you're getting an  
> arrarref of hashref structure.
> That is a reference to array of rows that itself is a reference to  
> a hash of column name/column values.
> If you use Data::Dumper again you'll see the structure looks like this
>
> $ra_results = [
>                 {
>                   'last' => 'Allman',
>                   'first' => 'Duane'
>                 },
>                 {
>                   'last' => 'Vaughn',
>                   'first' => 'Stevie Ray'
>                 },
>               ];
>
>
> You would loop through that structure like this -
>
>     my $ra_results = $sth->fetchall_arrayref({});
>
>     for my $ra_row (@{$ra_results}) {
>         for my $key (keys %{$ra_row}) {
>             my $value = $ra_row->{$key};
>            ...( more code here ) ...
>         }
>     }
>
>
> If you're using TMPL_LOOP's you probably don't need to be munging  
> around with that data structure anyway since you need to assign an  
> arraryref of hasref to the param -
>
>    $template->param(EMPLOYEE_INFO => $ra_results );
>
>
> Check out the perl doc perldsc (Data Structures Cookbook) for a  
> discussion on data structures and derefernencing.
>
> Good luck
> Steve
> (Programmer, not soda jerk :)
>
>
> Steve Ragan
> Sr. Internet Developer
> Harris Connect
> 2500 Westchester Ave.
> Purchase, NY 10577
> T 914.641.3948
> F 914.641.3584
> [email protected]
> www.harrisconnect.com
>
>
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]]On Behalf Of
> Thomas Nyman
> Sent: Friday, August 12, 2005 5:11 AM
> To: [email protected]
> Subject: [htmltmpl] dereferencing issues
>
>
> Hi
>
> I've tried studying perldoc pages, the htmps template pages and most
> things relating to dbi etc.
>
> Despite this I have been unable to understand how to dereference
> fetchall_arrayref({})
>
> I can dereference fetchall_arrayref()  but not  fetchall_arrayref
> ( {} ), i.e the arrayref of hash refs.
>
> I would like to query the database using fetchall_arrayref({}), the
> dereference that and perhaps make changes to some values, put it back
> into  an arrayref({}) and then dump it to html template.
>
> SO far I'm stumped with the first part..i simply cant dereference the
> *fg!?*-thing  <grinning> .. anyway, please is there some who can help
> iterate through arrayref({}) (both key and value).
>
> AN example
>
> I query the database for "Name", "Adress", "Phone", "Fax", "Email"
>
> the $rows = $sth->fetchall_arrayref({});
>
> now i would like to dump my rows into an array and check for certain
> values etc, for instance lets say the phonenumber is represented as
> 512445566 in the database, but i want it to display as 512-445566 in
> the html-template..therefore i would need to access the "Phone"
> query, check its format, possibly change it, insert the new value in
> my arrayref of hashrefs.
>
> (on my knees and waiting)
>
> Thomas
>
>
>
> -------------------------------------------------------
> SF.Net email is Sponsored by the Better Software Conference & EXPO
> September 19-22, 2005 * San Francisco, CA * Development Lifecycle  
> Practices
> Agile & Plan-Driven Development * Managing Projects & Teams *  
> Testing & QA
> Security * Process Improvement & Measurement * http://www.sqe.com/ 
> bsce5sf
> _______________________________________________
> Html-template-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/html-template-users
>
> ********************************************************************** 
> ********************************************************************** 
> ****
> This message (including any attachments) contains confidential  
> information intended for a specific individual and purpose, and is  
> protected by law.  If you are not the intended recipient, please  
> delete this message. Any disclosure, copying, or distribution of  
> this message, or the taking of any action based on it, is strictly  
> prohibited.
>
> NOTE:  Effective immediately, our name has changed from Bernard C.  
> Harris Publishing Company to Harris Connect. As a result, our e- 
> mail domain name is changing from [email protected] to  
> [email protected].  Please make the appropriate adjustments to  
> any personal address book entries or contact information.
>



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.