http://qs321.pair.com?node_id=120803

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

I would like to do something like push(@ary,\%hash) instead of @ary = (\%hash); so that
all the values will be in @ary.

I cannot get it to work. Any idears???

Soren

#!/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/search.tmp +l"; my $tmpl = new HTML::Template( filename => TMPL_FILE ); my $search = $cgi->param("search"); my $rapport = $cgi->param("rapport"); my $dbh = DBI->connect("um","","","mysql") || die "Could not connect: $DBI::errstr\n"; my %attr = ( dbi_fetchall_arrayref_attr => {} ); my @selector; if ($rapport eq undef) { @selector = qw(fornavn efternavn cpr adresse zip city telefon mobil g_ +afsluttet bemyndigelse_til tjenestested id pic godkendelse_done); } else { @selector = qw(fornavn efternavn cpr bemyndigelse_til tjenestested god +kendelse_done g_afsluttet); } my @s = split(/ /, $search); my $row = $dbh->selectall_arrayref('select '. join(',' , @selector)." +from people where ((fornavn like '%$s[0]%') and (efternavn like '%$s[$#s]%')) or ( +fornavn LIKE '%$search%') or (efternavn LIKE '%$search%') or (cpr LIK +E '%$search%')",\ %attr); my %hash; my @ary = @$row; my ($i, $k, $v, $date); for ($i=0; $i<=$#ary; $i++) { %hash = %{$ary[$i]}; $date = con_date($hash{"g_afsluttet"}); $hash{"g_afsluttet"} = $date; }

HELP NEEDED HERE :o)
@ary = (\%hash);
HELP NEEDED HERE :o)
#--------Farven findes start ----------------- my $color; if ($hash{"godkendelse_done"} =~ '1') { $color = 'white'; } else { $color = 'red'; } #--------Farven findes slut ------------------ #----------Data slettes start ---------------- if ($rapport ne undef) { delete $hash{"g_afsluttet"}; } delete $hash{"godkendelse_done"}; #----------Data slettes slut ----------------- $tmpl->param( search => $search, rapport => $rapport , row => \@ary , +color => $color ); print "Content-type: text/html\n\n", $tmpl->output; $dbh->disconnect; sub con_date { my ($org_date) = @_; my ($yyyy, $mm, $dd) = ($org_date =~ /(\d+)-(\d+)-(\d+)/); my $ret = sprintf ("%02d-%02d-%04d", $dd, $mm, $yyyy); return $ret; }

Replies are listed 'Best First'.
Re: push(@ary,\%hash)
by tachyon (Chancellor) on Oct 23, 2001 at 19:26 UTC
    @ary = values %hash; # assign all values in %hash to @ary push @ary, values %hash; # add all values in % hash to end of @ary

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: push(@ary,\%hash)
by shadox (Priest) on Oct 23, 2001 at 19:14 UTC
    Hi there, correct me if i am wrong, what you want to do is assign to @ary all the values of %hash? Well, if i am right, what U are doing here.
    @ary = (\%hash);
    Is passing a hash reference to @ary, lets supose %hash have the following:
    %hash = ( 'foo' => 'bar', 1 => 2 ); # with this line @ary = (\%hash); # i would need to do something like this to access the values print $ary[0]->{'foo'}; # And i will get 'bar' print $ary[0]->{1}; # And i will get 2 # But if you just want to get all the values from the hash #to the array, just remove the "\" when you assign it @ary = (%hash);
    I hope to be right and answer that you need
    </CODE>
    Dreams they just disapear into the shadows,
    then they become true....
Re: push(@ary,\%hash)
by buckaduck (Chaplain) on Oct 23, 2001 at 19:32 UTC
    Replace this code:
    my %hash; my @ary = @$row; my ($i, $k, $v, $date); for ($i=0; $i<=$#ary; $i++) { %hash = %{$ary[$i]}; $date = con_date($hash{"g_afsluttet"}); $hash{"g_afsluttet"} = $date; } HELP NEEDED HERE :o) @ary = (\%hash); HELP NEEDED HERE :o)
    With this:
    my @ary; foreach my $eachrow (@$row) { my %hash = %{$eachrow}; $hash{g_afsluttet} = con_date($hash{g_afsluttet}); push @ary, \%hash; }

    buckaduck

Re: push(@ary,\%hash)
by DrManhattan (Chaplain) on Oct 23, 2001 at 21:48 UTC
    I'm not entirely sure what you're trying to accomplish, but I'll give it a shot. :)
    my $row = $dbh->selectall_arrayref(...)
    $row will be a reference to an array of arrayrefs. E.g. ...
    $row->[0]->[0]
    ... is the first column of the first row of output. If you want an array of hashrefs, use selectall_hashref():
    my $row = $dhb->selectall_hashref(...)
    $row->[0]->{'g_afsluttet'} is the column named 'g_afsluttet' in the first row of output. Now, if I understand your question right, you want to modify one of the columns of your output. You want to replace the value of the column named 'g_afsluttet' with con_date(<value>) for every row in the output? If that's the case, you can do it like this:
    my $rows = $dbh->selectall_hashref(...); ... foreach my $hashref (@$rows) { $hashref->{'g_afsluttet'} = con_date($hashref->{'g_afsluttet'}); } ... $tmpl->param(LOOP => $rows);

    -Matt