http://qs1969.pair.com?node_id=613645


in reply to Re^2: Populating an array from a mysql select
in thread Populating an array from a mysql select

Allow me to explain.

fetchall_arrayref returns an array ref, i.e. a scalar value which is a reference to an array (which will stringify as something like ARRAY(0x81503e8) if you try and print it out). You want to dereference this, and gain access to the array, hence the @{...}. This array has one element per row.

Unfortunately, this is not quite exactly what you want, as it's an array of arrayrefs. Each of the referenced arrays only has one element, as your SQL is only returning one column, but would contain more than one value if your query was returning more columns (note: there is no such method as fetchcol_arrayref).

The trick I have done is to fish out the element of each array, as the action for the map function. I could have written this as a for loop, but this kind of inline transformation lends itself to the builtins map and grep.

For further reading, see perlreftut.

--
wetware hacker
(Qualified NLP Practitioner and Hypnotherapist)

Replies are listed 'Best First'.
Re^4: Populating an array from a mysql select
by Nik (Initiate) on May 04, 2007 at 19:43 UTC
    An array reference is actually a pointer(as we would have said in C++) that points to the starting memoery location of the array?! And de-reference means to point to the value of memory location instead of the memory location itself?

    Also this: my @titlelist; open FILE, "<$ENV{'DOCUMENT_ROOT'}/data/vault/titlelist.txt" or die $!; @titlelist = <FILE>; close FILE; print br() x 3; print start_form( action=>'/cgi-bin/admin.pl' ); print table( {class=>'user_form'}, Tr( td( '&#928;&#941;&#962; &#956;&#959;&#965; &#964;&#953; &#95 +2;&#945; &#942;&#952;&#949;&#955;&#949;&#962;:' ), td( popup_menu( -name=>'title' -values=>\@titlelist ))), Tr( td( '&#922;&#940;&#964;&#953; &#940;&#955;&#955;&#959; &#960 +;&#959;&#965; &#952;&#945; &#942;&#952;&#949;&#955;&#949;&#962; &#957 +;&#945; &#963;&#967;&#959;&#955;&#953;&#940;&#963;&#949;&#953;&#962;? +' ), td( textarea( -name=>'remark', -rows=>4, -columns=>25 ))), Tr( td( '&#932;&#959; &#964;&#951;&#955;&#941;&#966;&#969;&#957; +&#959; &#949;&#960;&#953;&#954;&#959;&#953;&#957;&#969;&#957;&#943;&# +945;&#962; &#963;&#959;&#965; &#947;&#953;&#945; &#949;&#960;&#953;&# +946;&#949;&#946;&#945;&#943;&#969;&#963;&#951; &#949;&#943;&#957;&#945;&#953;:' ), td( textfield( -name=>'phone' )) +), Tr( td( a( {href=>'/cgi-bin/show.pl?name=showbook'}, font( {size=>3, color=>'yellow'}, '&#917;&#956;&#966;&#940;&#957;&#95 +3;&#963;&#951;!' ))), td( submit( '&#913;&#960;&#959;&#963;&#964;&#959;&#955;&#942 +;!' ))) ); print hidden(-name=>'date', -value=>$date); print hidden(-name=>'host', -value=>$host); print end_form();
    Although @titlelist is filled with all entries from titlelist.txt file(one entry per line), when it gets printed with popup_menu it displays nothing at all!!

      Pointers and references are similar but distinctly different beasts; for example pointers can be manipulated (e.g. you add one to get to the next item), whereas references are opaque (it doesn't make sense to say $array_ref += 1). A read of perlreftut may be in order before reading perlref.

        Pointers and references are similar but distinctly different beasts

        I have a strange sensation, just as if I had already answered that question elsewhere. But I'm getting tired pointing out so.