Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of references - what am I doing wrong?

Status
Not open for further replies.
Jan 15, 2008
1
US
I am trying to create an array of references to arrays.

When I run the code below, it shows the correct data being pushed onto the array, but when I print it out at the end, every value is "3".

Any ideas?

Thanks,

Steve


for ($i=0; $i < @key_columns; $i++) {
if ($i == 0) {
$where = " where $key_columns[$i] = ?";
} else {
$where .= " and $key_columns[$i] = ?";
}
}

$sql_template = "select $key from $table $where";
$sth = $dbh->prepare($sql_template);
print "Prepared SQL is $sql_template\n";
$cnt = 0;
for $row (@$rows) {
$cnt++;
print "$cnt: @$row\r";
$got_row = 0;
print "Calling execute on @$row\n";
$sth->execute(@$row);
$primary_keys = $sth->fetchall_arrayref;
$rows_retrieved = int(@$primary_keys);
print "Got back $rows_retrieved\n";
$cnt += $rows_retrieved;
$got_row = 1 if @$primary_keys > 0;
for $primary_key (@$primary_keys) {
print "pushing @$primary_key\n"; # This data prints out correctly
push @new_keys, [ @$primary_key ];
# push @new_keys, $primary_key; - I also tried it like this, same result

}

# If we can't match the key from the key table with a row in the original table (probably due
# to corruption), push that key into the list of primary keys, anyway; we can try matching
# against the backup tables later...

if (! $got_row && $keys_same) {
push @new_keys, $row;
}
}
for $primary_key (@new_keys) { # This prints out wrong
print "key: " . @$primary_key . " $primary_key \n";
}
 
The concatenation operator puts your array in a scalar context, therefore returning the number of elements. As prex points out, if you just interpolate the array like you did in your first statement, it should work fine. My personal preference is to always use an explicit join when printing an array, but that's just me.

Also, I would strongly encourage you to start putting use strict; at the top of your script and declaring your variables. It's always a good idea, but when you use variable names that differ in only an 's' suffix, you're even more likely to save you headache if you let perl help you.

Finally, here's a cleaner method to write your beginning where construction:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$where[/blue] = [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url] [red]'[/red][purple] AND [/purple][red]'[/red], [url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][red]"[/red][purple][blue]$_[/blue]=?[/purple][red]"[/red][red]}[/red] [blue]@key_columns[/blue][red];[/red]
[blue]$where[/blue] &&= [red]"[/red][purple] WHERE [blue]$where[/blue][/purple][red]"[/red][red];[/red]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top