in reply to Re: array to string
in thread array to string

one thing confuses me is that the query is ok, but perl not coverting into to string and print out, because when i use data dumper to print info, there no problem info comes

i tried to select with the actual location and results came

$VAR1 = [ [ 1 ] ]; Content-type: text/html

other country with no location also results come ok

$VAR1 = [ [ 0 ] ]; Content-type: text/html

Replies are listed 'Best First'.
Re^3: array to string
by stevieb (Canon) on Oct 13, 2020 at 18:01 UTC

    What you've got there is an array reference with one element, which is another array reference with a single element. Change your code to this:

    for my $total_info(@$Count) { print "$total_info->[0]\n"; }

    In the for statement, you extract a single element of the array in a loop (the inner array reference). The print statement prints out the first element of the inner array reference.

    Will you ever get more than one record returned per call? If not, you might consider not using fetchall_arrayref, as that's why you have an aref inside of a top-level aref. fetchall_* implies that you're expecting several records returned. If you only ever expect one piece of data returned per call, use fetchrow_arrayref.

      The output of SELECT COUNT(*)... should always be a single number, so the outer foreach is probably unnecessary:

      print "$Count->[0]->[0]\n";

      Dave

      For SELECT COUNT(*) FROM ... and similar queries that can only return a single value in a single row, the selectrow_array method can directly return the desired information, instead of wrapping it into an arrayref. The DBI documentation has more details, including using that method with previously prepared statements.

      thanks guys