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;
}
result
$#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"
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.