On Mon, 14 Mar 2005 03:50:19 +0100, John Doe <[EMAIL PROTECTED]> wrote: > Am Montag, 14. März 2005 03.30 schrieb Grant: > > > > Thanks guys, I've almost got it. I need to save the title and URL of > > > > each result in the array to a different "scratch" variable so I can > > > > use it outside of the script. The following works great, but of > > > > course it only saves the last set of results from the loop. > > > > foreach my $result (@{$results->{resultElements}}) { > > > > $Scratch->{google_results_title} = $result->{title}, > > > > $Scratch->{google_results_url} = $result->{URL}; > > > > } > > > > > > > > What I need is a way to save each title and URL to a different scratch > > > > variable like this: > > > > > > > > $Scratch->{google_results_title_1} = > > > > $results->{resultElements}->[1]->{title}, > > > > $Scratch->{google_results_url_1} = > > > > $results->{resultElements}->[1]->{url}; > > > > $Scratch->{google_results_title_2} = > > > > $results->{resultElements}->[2]->{title}, > > > > $Scratch->{google_results_url_2} = > > > > $results->{resultElements}->[2]->{url}; > > > > > > > > but I need the right syntax. How would that go?
Depends what you mean by "outside the script" is this a package or object you will be calling from within another script? Is it a cgi script where you want to pass the values to another cgi script? Or are you storing the data, at least temporarily, to disk and parsing it later with an unrelated script? To put it another way: once you've built the hash, what's going to happen to it, and how is it going to become persistent data that can move "outside the script"? All of these situations require slightly different solutions. My gut reaction would be to save everything to a (G)DB file: use DB_File; my %RESULTS; my $resultsfile = "/path/to/file" ; tie(%RESULTS, "DB_File", $resultsfile) or die "can't open $resultsfile: $!\n" ; foreach $result (@{$results->{resultElements}}) { $RESULTS{$result->{URL}} = $result->{title} } untie %RESULTS ; I've reversed the keys and values here because the url is guaranteed to be a unique hash key, but the titles could be duplicated. Now you can use the normal has operations to manipulate your data from any script. This assumes that you want the data sved to disk, accessable as a hash. If you want a more complex data structure written to disk, look into Storable. And if you need to pass via cgi, you'll want to write the hash back out into hidden fields. HTH, --jay -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>