Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: DB search results using DBI...execute

by huck (Prior)
on Mar 04, 2017 at 21:44 UTC ( #1183674=note: print w/replies, xml ) Need Help??

in reply to DB search results using DBI...execute

use strict; use warnings; use Data::Dumper; my @arr=({a=>1,b=>2},{a=>3,b=>4},{a=>5,b=>6}); print '$#arr :'.$#arr."\n"; print 'scalar(@arr):'.scalar(@arr)."\n"; print Dumper \@arr; my @results = rvar('hash') . "\n"; print Dumper \@results; print "Number of array elements: <b>" . @results . "</b><br>\n"; my @results2 = rvar('arr') . "\n"; print Dumper \@results2; print "Number of array elements: <b>" . @results2 . "</b><br>\n"; sub rvar { my $mode=shift; my @arr=({a=>1,b=>2},{a=>3,b=>4},{a=>5,b=>6}); if ($#arr eq 2) {print "text eq works but weird\n"; } if ($mode eq 'hash') { my $hashRef = $arr[0]; return $hashRef; } elsif ($mode eq 'arr') { my $arrayRef = \@arr; return $arrayRef; } return 0; }
$#arr :2 scalar(@arr):3 $VAR1 = [ { 'b' => 2, 'a' => 1 }, { 'b' => 4, 'a' => 3 }, { 'a' => 5, 'b' => 6 } ]; text eq works but weird $VAR1 = [ 'HASH(0x3f7f2c) ' ]; Number of array elements: <b>1</b><br> text eq works but weird $VAR1 = [ 'ARRAY(0xb321bc) ' ]; Number of array elements: <b>1</b><br>
First, $#arr returns the index of the last element, not the count.
Second all you ever return from sql_execute is a scalar, it may be a hashref, or a arrayref, or a number but it is always a scalar.
Third, in my @results = search_item($dbh, \@users, $st, $sv) . "\n"; you force scalar mode with the . for concatenation, and the "\n" gets placed into the @results array appended to scalar result as its only item.
Fourth, you never showed us search_item so i am just sortof guessing but if search_item actualy did return an array the size of the array would be concatenated with the "\n" since scalar context was forced

Edit: added "index of the" to "returns the index of the last element"

Replies are listed 'Best First'.
Re^2: DB search results using DBI...execute
by jamroll (Beadle) on Mar 04, 2017 at 21:49 UTC
    my oops - here's the "search_item" code...
    sub search_item($$$$) { #* # to search for a list of user IDs which match one criteria (ie: eye +_clr=1) # searches can only happen with the logged in user # we must not include the logged in user in the search results! # nor should we include those that are banned or security level 888 +or higher #* my ($db, $usersRef, $name, $value) = @_; # a DBH && a reference to a + list of users to search && the name of the item to search for && the + value the named item must be my @searched; # a list of UIDs which match the given name=value pair +s my $query = "select ID from users where $name='$value';"; my $resultsRef = sql_execute($db, $query); if ($resultsRef) { if (ref $resultsRef eq "ARRAY") { # we have multiple hits! my @array = @$resultsRef; foreach my $hit (@array) { # $hit is a hash reference. my %hash = %$hit; push @searched, $hash{ID}; } } else { # we only have one hit! my %hash = %$resultsRef; push @searched, $hash{ID}; } } # it is very possible to have an empty "searched" array. # this is totally ok. return @searched; # an array of uid's which matched the specified cr +iteria, minus the logged in user's uid #usage: my @results = search_item($db, \@uList, "eyes", "1"); }
Re^2: DB search results using DBI...execute
by jamroll (Beadle) on Mar 04, 2017 at 21:55 UTC
    reviewed my code, and sure enough, that damn "\n" is there! now i feel REALLY dumb! lol - TYVM, huck, for pointing that out! i don't have a CLUE how i did that. Likely due to commenting, and modifications, and just missed it. now, i get a list of UID's, and not just a 3. Thank you, thank you, thank you. I think i can finally move onto the next stage. MOST appreciated, Sire.

      Dont forget to fix the if ($#arr eq 1) { test too. its either if ($#arr eq 0) { or if (sclalar(@arr) eq 1) {

        wow. so many different ways to do things, makes it hard to keep track of which is right lol. ok. so, i changed it from $#arr to scalar(@arr) i'll have to rip through some 30,000 lines of code to fix that issue. do i have to use if (scalar(@arr) eq 1) or would if (@arr eq 1) work the same?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2021-12-09 11:01 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (36 votes). Check out past polls.