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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting and printing variables from array

Status
Not open for further replies.

mrimagepueblo

Programmer
Dec 15, 2003
52
US
I am attempting to print the results of an array, @database rows which shows the results of 5 records in my database. I need to populate showAddress with each record in the array to put those fancy markers in a google map. If I manually put showAddress in the spot below 5 times with 5 address the markers show perfectly. For reasons beyond me, it's knocking off the first 4 entries. I put in some code to print to the file debugging.txt and it's writing only the last record of the 5 search results.

I'm working with an existing program and have a little understanding of perl, but not enough to make it work.

debugging.txt has this in it's content. (some of the record is truncated as it's very long.

105535|1950000|R|A|1306|S||Liberty Point Blvd|Pueblo West|CO|81007|7|6|12|47|


code snippet:

sub search_results_header
{
print qq~
<head>
head code
</head>

foreach $key (keys(%db))
{
if ($form_data{$key} ne "")
{
print qq~
<INPUT TYPE = "hidden" NAME = "$key"
VALUE = "$form_data{$key}">
~;
}

}
}

sub search_results_body
{


if ($total_row_count >1)
foreach $row (@database_rows)
{
@fields = split (/\|/, $row);


<!-- routine to see what's in $row -->
open W,'>debugging.txt';
print W $row;
close W;
print "$row\n";
<!-- end routing to see what's in $row


<! -- display 5 search results -->

print qq~


<!-- begin google map javascript code -->

<script type="text/javascript">
//<![CDATA[


showAddress("$fields[4] $fields[5] $fields[7] $fields[8], $fields[9] $fields[10]","$fields[4] $fields[5] $fields[7]<br />$fields[8], $fields[9] $fields[10]");

<!-- end google map javascript code -->

//]]>
</script>


<!-- begin displaying map -->
<div id="map" style="width: 500px; height: 300px"> </div>
<!-- end displaying map -->


<!-- display code for thumbnail -->

code
code
code to display footer

-----------------------------------


utlimately in the javascript section I should have 5 occurences, one for each $row.

showAddress("$fields[4] $fields[5] $fields[7] $fields[8], $fields[9] $fields[10]","$fields[4] $fields[5] $fields[7]<br />$fields[8], $fields[9] $fields[10]");
showAddress("$fields[4] $fields[5] $fields[7] $fields[8], $fields[9] $fields[10]","$fields[4] $fields[5] $fields[7]<br />$fields[8], $fields[9] $fields[10]");
showAddress("$fields[4] $fields[5] $fields[7] $fields[8], $fields[9] $fields[10]","$fields[4] $fields[5] $fields[7]<br />$fields[8], $fields[9] $fields[10]");
showAddress("$fields[4] $fields[5] $fields[7] $fields[8], $fields[9] $fields[10]","$fields[4] $fields[5] $fields[7]<br />$fields[8], $fields[9] $fields[10]");
showAddress("$fields[4] $fields[5] $fields[7] $fields[8], $fields[9] $fields[10]","$fields[4] $fields[5] $fields[7]<br />$fields[8], $fields[9] $fields[10]");


 
Debugging:
- Where possible, use the my() construct in your subroutines.
- Not knowing how %db or %form_data were obtained, be sure you're not duplicating $key vars. Ex:
Code:
   # assume FH is a generic CSV type file.
   while ($line = <FH>) {
      ($key, $value) = split(/\s*,\s*/, $line);
      chomp $value ; # if needed, ask why this is after the split
      if (defined $db{$key}) {
         print "Warning: Duplicate key found >$key<\n";
      };
      $db{$key} = $value ;
   };
- It also sometimes helps to do a sort when working with (keys %hash_var). The sort helps determine which entries are missing. Sometimes the missing entries have a common characteristic.
 
I guess you need to determine what is in your array. Just print each row of the array for now to verify its contents.

Code:
foreach $row (@database_rows)
  print "$row\n";
}

If all the rows are there then you need to start working on the javascript/html output and see if the problem is there somewhere. Trying to help a person with a snippet of code from a larger program that appears not written with the benefit of the "strict" pragma is near impossible.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
form_data is coming from db-lib.pl, and the program also uses cgi-lib.pl. I know, those programs are as old a dirt, but it has worked well for my requirements to this point.

For each row the current row shows when I print
print "$row\n";

which is what's expected at this point.

So what I'm guessing, it's probably not in the code above at all? It would be in the db-lib.pl library routine where I would create an array of the results, and then print the array in the javascript area?

There is a section in db-lib.pl where the array is built?

if ($not_found == 0)
{
push(@database_rows, join("\|", @fields));
}

 
mrimagepueblo said:
I put in some code to print to the file debugging.txt and it's writing only the last record of the 5 search results.
Er, that's because you are opening and closing the file each time you go through the loop. As you overwrite the file each time, you will only ever see the last record...

Also, if you have complex data structures that you want to examine, use Data::Dumper
Perl:
use Data::Dumper;
..
..
print Dumper($myHashOfArrays);
for example.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top