Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

array to string

by bigup401 (Pilgrim)
on Oct 13, 2020 at 17:22 UTC ( #11122776=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: array to string
by davido (Cardinal) on Oct 13, 2020 at 17:29 UTC

    What happens when you use Data::Dumper and dump $Count?

    use Data::Dumper; # do your select.... # then... print Dumper $Count;

    If there's a misunderstanding about the data structure that is making it hard to print it, this will given you insight.


      thank you

      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

        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.

Re: array to string
by stevieb (Canon) on Oct 13, 2020 at 17:48 UTC

    This is not converting an array to a string. To do that, one would typically use the join function.

    You're simply iterating over an array (reference) and printing out each element.

    You've been here quite some time, yes? Why don't you have your expected output and actual output or errors listed in your question?

Re: array to string
by NetWallah (Canon) on Oct 13, 2020 at 18:00 UTC
    Running a similar query on my system with the perl debugger shows this structure:
    DB<3> x $Count 0 ARRAY(0x55a8d92fb240) 0 ARRAY(0x55a8d9613bb0) 0 5
    This change to your code displays the expected "5":
    for my $total_info(@{$Count->[0]}) { print $total_info,"\n"; }
    Update: If you want to print the value directly, without a loop:
    print "Non-destructive = ", $Count->[0][0],"\n";
    Update: Another way to directly print the content (Destructive, and NOT RECOMMENDED):
    print "Double shift = ",shift @{shift @$Count} ,"\n";

                    "Imaginary friends are a sign of a mental disorder if they cause distress, including antisocial behavior. Religion frequently meets that description"

Re: array to string
by AnomalousMonk (Bishop) on Oct 13, 2020 at 19:09 UTC

    The knee-jerk link whenever anyone makes even the barest mention of an array/hash structure of even the slightest complexity: the Perl Data Structures Cookbook (perldoc perldsc on the command line).

    Give a man a fish:  <%-{-{-{-<

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11122776]
Approved by davido
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-09-26 02:48 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (116 votes). Check out past polls.