Re: Getting my head round hashes
David, You don't have a response set at execute. You have a reference to a responce set. You need to use DBI methods to access the results. Try this... $response-execute(); my $hash = {}; while (my $rows = $response-fetchrow_hashref){ $hash-{$rows-{task_ID}} = $rows; } $response-finish(); use Data::Dumper; print Dumper $response; --- Greenhalgh David [EMAIL PROTECTED] wrote: Hi all, I have a script which mostly works, but when I get to the part with hashes, it stops. I'm obviously doing something very basically wrong, but I can't see what. (been staring at so long it could be anything.) A segment of the code is below: #!/usr/local/perl -wT use strict; use CGI; # Do some stuff to identify the value of $marker which will be a number # Do MySQL query to grab the data to be manipulated my $dbh=connect(DBI:mysql:database, user, password); my $query=(SELECT task_ID, priority FROM task_list WHERE priority $marker); my $reponse-prepare($query); $response-execute; # $ response should now contain a # reference to a hash filled with task_ID, priority pairs # Dereference $response my %hash=%($response); $response-finish; # Subtract 1 from each value in the hash my ($key, $priority, %newhash); foreach $key (keys(%hash)){ my $priority=$hash{$key}--; %newhash=($key, $priority); } # Put the new $priority values into the MySQL table $query=(UPDATE task_list SET priority=? WHERE task_ID=?); $response-prepare($query); foreach $key (keys{%newhash}){ $priority=$newhash{$key}; $response-execute($priority, $key); } $response-finish; # Test the result print Content-Type: text/html\n\n; $query=(SELECT task_ID, priority FROM task_list); $response-prepare($query); $response-execute; while($key, $priority){ print Priority: $priority Task Ident: $keybr\n\n; } Would someone be patient enough to point out the mistake? Thanks Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] = -BEGIN GEEK CODE BLOCK- Version: 3.12 GIT d s+:++ a C++ UL++ US+ P+++ L++ W+++ w PS PE t++ b+ G e r+++ z --END GEEK CODE BLOCK-- __ Do you Yahoo!? Yahoo! Calendar - Free online calendar with sync to Outlook(TM). http://calendar.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Getting my head round hashes
Kristofer, Thank you. It was a big mistake to forget the method. I think I still have problems with catching when to use $ or %. I don't fully understand this section of the code: my $hash = {}; while (my $rows = $response-fetchrow_hashref){ $hash-{$rows-{task_ID}} = $rows; } Why is hash declared as my $hash and not my %hash? and how does the final line result in a hash? I am sorry, these are basic questions but I haven't got the hang of the logic yet. Dave On Thursday, June 5, 2003, at 07:25 pm, Kristofer Hoch wrote: David, You don't have a response set at execute. You have a reference to a responce set. You need to use DBI methods to access the results. Try this... $response-execute(); my $hash = {}; while (my $rows = $response-fetchrow_hashref){ $hash-{$rows-{task_ID}} = $rows; } $response-finish(); use Data::Dumper; print Dumper $response; --- Greenhalgh David [EMAIL PROTECTED] wrote: Hi all, I have a script which mostly works, but when I get to the part with hashes, it stops. I'm obviously doing something very basically wrong, but I can't see what. (been staring at so long it could be anything.) A segment of the code is below: #!/usr/local/perl -wT use strict; use CGI; # Do some stuff to identify the value of $marker which will be a number # Do MySQL query to grab the data to be manipulated my $dbh=connect(DBI:mysql:database, user, password); my $query=(SELECT task_ID, priority FROM task_list WHERE priority $marker); my $reponse-prepare($query); $response-execute; # $ response should now contain a # reference to a hash filled with task_ID, priority pairs # Dereference $response my %hash=%($response); $response-finish; # Subtract 1 from each value in the hash my ($key, $priority, %newhash); foreach $key (keys(%hash)){ my $priority=$hash{$key}--; %newhash=($key, $priority); } # Put the new $priority values into the MySQL table $query=(UPDATE task_list SET priority=? WHERE task_ID=?); $response-prepare($query); foreach $key (keys{%newhash}){ $priority=$newhash{$key}; $response-execute($priority, $key); } $response-finish; # Test the result print Content-Type: text/html\n\n; $query=(SELECT task_ID, priority FROM task_list); $response-prepare($query); $response-execute; while($key, $priority){ print Priority: $priority Task Ident: $keybr\n\n; } Would someone be patient enough to point out the mistake? Thanks Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] = -BEGIN GEEK CODE BLOCK- Version: 3.12 GIT d s+:++ a C++ UL++ US+ P+++ L++ W+++ w PS PE t++ b+ G e r+++ z --END GEEK CODE BLOCK-- __ Do you Yahoo!? Yahoo! Calendar - Free online calendar with sync to Outlook(TM). http://calendar.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Getting my head round hashes
You're not pulling in the DBI functions, for one thing. Short example of using the hashref function built into DBI is shown below. #!/usr/local/perl -wT use strict; use CGI; use DBI; # need this! my $marker = 5; (or whatever number) my $dbh=DBI-connect(DBI:mysql:database:localhost,user,password); my $sth=$dbh-prepare(SELECT task_ID, priority FROM task_list WHERE priority ?); $sth-execute($marker); while(my($hashref) = $response-fetchrow_hashref) { $hashref-{priority}--; # decrement each priority by 1 } $sth-finish; $dbh-disconnect; Scot R. inSite -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Getting my head round hashes
Thanks Scott, So %hashref now has the modified list of priorities with the task_ID as the key. I think I'm beginning to get the hang of this... I do have use DBI in the code (clipped it by accident when I extracted the segment) but you are quite right about me missing the the fetchrow_hashref method. D'Oh. Dave On Thursday, June 5, 2003, at 07:55 pm, Scot Robnett wrote: Sorry, forgot to comment something...yeeesh #!/usr/local/perl -wT use strict; use CGI; use DBI; # need this! my $marker = 5; # (or whatever number) my $dbh=DBI-connect(DBI:mysql:database:localhost,user,password); my $sth=$dbh-prepare(SELECT task_ID, priority FROM task_list WHERE priority ?); $sth-execute($marker); while(my($hashref) = $response-fetchrow_hashref) { $hashref-{priority}--; # decrement each priority by 1 } $sth-finish; $dbh-disconnect; Scot R. inSite -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Getting my head round hashes
On Thursday, Jun 5, 2003, at 21:43 US/Pacific, Mark Young wrote: [..] One nice way to learn about hashes, arrays, scalars and references, is to learn the perl debugger and just experiment. There is a perl debugger tutorial in the perl documentation. [..] My Compliments! cf perldoc perldebug perldoc perldebtut folks would also be wise to check out perldoc Data::Dumper since these will help the user sort out whether the problem is with some piece of perl arcania, or is it really the more complex problem of how their cgi code is working! Folks should never be afraid to just write a little piece of perl, whip it in the debugger, and see if it is doing what must be done... To help 'debug' the html side of the problem, one either needs to have a. their web-server set to full debug mode, and tail the logfile b. cut a simple mini-web-server that is in debug mode c. run simple scripts to act like a web-server... To help, I have a small /bin/sh script I use: vladimir: 57:] sed 's/^/###/' ~/bin/test_cgi !/bin/sh ### ###METHOD=$1 ###SCRIPT=$2 ###QUERY_STRING=$3 ###DEBUG=$4 ### ###REQUEST_METHOD=${METHOD} ###dir=`pwd` ###dir=`echo $dir | sed s'!.*\(/home\)!\1!'` ###SCRIPT_FILENAME=${dir}/$SCRIPT ### ###export REQUEST_METHOD SCRIPT_FILENAME QUERY_STRING ### echo ${METHOD} ${SCRIPT} ${DEBUG} ### ###perl ${DEBUG} $SCRIPT vladimir: 58:] which I call with test_cgi GET name_of_script query_string [ -d ] say : vladimir: 62:] test_cgi GET index.cgi callForm=bobfran=zoie Content-Type: text/html Content-Length: 487 !DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN htmlheadtitleHQ Admin Web Tool on vladimir/title/head frameset border=0 cols=135,* frameborder=no framespacing=0 frameset border=0 rows=97,* frameborder=no framespacing=0 frame name=HqIcon scrolling=no src=html/icon.html frame name=VertNav src=L1/L1_VertNav.cgi /frameset frame name=MainBody src=L1/L1_MainBody.cgi?callForm=bobfran=zoie /frameset/html vladimir: 63:] So that it 'acts' like a mini-web-server, the '-d' option of course would take us into the perl debugger itself... This will help get past the usual 'issues' with debugging perl cgi scripts, that want to have one 'input' stuff and type ^D or echo the query string in Your mileage may vary ciao drieux --- short shameful confession: It has been a long time since I have actually used a command line debugger for things like 'c' code, but fell into the need a while back, and myFascistHouseMate said, 'just use the gdb at the command line' and I did and laughed, hey, it's just like the perl debugger... and my professional peers all began to cry and move away from me on the Group W bench. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Getting my head round hashes
One nice way to learn about hashes, arrays, scalars and references, is to learn the perl debugger and just experiment. There is a perl debugger tutorial in the perl documentation. You can print out the arrays and hashes w/o putting print statements into your code, and that makes the experimentation very easy and interesting. Mark -Original Message- From: Greenhalgh David [mailto:[EMAIL PROTECTED] Sent: Thursday, June 05, 2003 1:16 PM To: [EMAIL PROTECTED] Subject: Getting my head round hashes Hi all, I have a script which mostly works, but when I get to the part with hashes, it stops. I'm obviously doing something very basically wrong, but I can't see what. (been staring at so long it could be anything.) A segment of the code is below: #!/usr/local/perl -wT use strict; use CGI; # Do some stuff to identify the value of $marker which will be a number # Do MySQL query to grab the data to be manipulated my $dbh=connect(DBI:mysql:database, user, password); my $query=(SELECT task_ID, priority FROM task_list WHERE priority $marker); my $reponse-prepare($query); $response-execute; # $ response should now contain a # reference to a hash filled with task_ID, priority pairs # Dereference $response my %hash=%($response); $response-finish; # Subtract 1 from each value in the hash my ($key, $priority, %newhash); foreach $key (keys(%hash)){ my $priority=$hash{$key}--; %newhash=($key, $priority); } # Put the new $priority values into the MySQL table $query=(UPDATE task_list SET priority=? WHERE task_ID=?); $response-prepare($query); foreach $key (keys{%newhash}){ $priority=$newhash{$key}; $response-execute($priority, $key); } $response-finish; # Test the result print Content-Type: text/html\n\n; $query=(SELECT task_ID, priority FROM task_list); $response-prepare($query); $response-execute; while($key, $priority){ print Priority: $priority Task Ident: $keybr\n\n; } Would someone be patient enough to point out the mistake? Thanks Dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]