http://www.perlmonks.org?node_id=119640

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I would like to add $tjeneste_type to $row.
Can anybody tell me how to do that??
BR Soren
************The Code**************
#!/usr/local/bin/perl -wT use strict; use HTML::Template; use CGI; use DBI; my $cgi = new CGI; use constant TMPL_FILE => "$ENV{DOCUMENT_ROOT}/um/templates/status.tmp +l"; my $tmpl = new HTML::Template( filename => TMPL_FILE ); my $search = $cgi->param("search"); my $tjenestested = $cgi->param("tjenestested"); my $dbh = DBI->connect("um","","","mysql") || die "Could not connect: $DBI::errstr\n"; my %attr = ( dbi_fetchall_arrayref_attr => {} ); my $row = $dbh->selectall_arrayref("select fornavn, efternavn, cpr, ad +resse, zip, city, g_afsluttet, bemyndigelse_til from people where (id + like '%$search%')",\ %attr); my $tjeneste_type; if ($tjenestested =~ /^Konsulent/) { $tjeneste_type = 'kon'; } else { $tjeneste_type = 'um'; } #ADD $tjeneste_type to $row??? $tmpl->param( row => $row ); print "Content-type: text/html\n\n", $tmpl->output; $dbh->disconnect;
Edit - Petruchio Thu Oct 18 10:34:45 UTC 2001: Added code tags, cleaned up markup.

Replies are listed 'Best First'.
Re: ADD $tjeneste_type to $row
by jeroenes (Priest) on Oct 18, 2001 at 14:35 UTC
    It seems you are having trouble with array refs. Read up on it in perlref,perllol,perldsc and perlreftut (link at my homenode).

    In general, to add an item to an arrayref:

    $array=[42]; push @$array, 666; #now $array=[42,666]
    And the truth has become evil.

    Jeroen
    "We are not alone"(FZ)

      See also References Quick Reference. It isn't a good introduction nor tutorial, but it can certainly crystalize the subject matter and make it a lot easier to remember (IMHO). It is also a nice quick reference when you don't use references very often and need to construct a new type of dereference but can't remember the rules.

              - tye (but my friends call me "Tye")
Re: ADD $tjeneste_type to $row
by cfreak (Chaplain) on Oct 18, 2001 at 19:20 UTC
    I'm assuming that row is a hash reference? If so then all you need to do is:
    $row->{tjeneste_type} = $tjeneste_type;

    That adds a new key to your hash reference and enters its value.

    Also you might be able to simplify things by not using an array reference at all. DBI has a nifty little method fetchrow_hashref which as the name implies automatically gives you a hash reference with the field names as keys.

    Of course all this is assuming that's what $row is. :)