Can someone tell me what is wrong with this?

Discussion in 'Perl Misc' started by Big Swifty, Jan 29, 2004.

  1. Big Swifty

    Big Swifty Guest

    I can't seem to find any examples that look like what I'm doing, so I'm
    sure I'm doing it wrong!!!
    I get back some rows from the database, and then want to put those values
    in checkboxes that if selected, will be then be used to select specific
    rows from another table.
    The first line works with the variables, but the checkbox line does not!!
    I tried quoting and unquoting and requoting etc etc!!!
    Where am I going wrong???


    THANKS IN ADVANCE!!!


    BIG SWIFTY


    .........

    foreach (@documents) {
    ($doc_id,$doc_name,$score) = $documents[$counter+0], $documents
    [$counter+1], $documents[$counter+2];
    print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";
    # PREVIOUS LINE WORKS
    print $q->checkbox (-name =>$doc_name,-value => "YES", -label =>
    $doc_name);
    # NOT LINE ABOVE
    $counter ++;
    }
     
    Big Swifty, Jan 29, 2004
    #1
    1. Advertising

  2. Big Swifty

    Guest

    Big Swifty <> wrote:
    > I can't seem to find any examples that look like what I'm doing, so I'm
    > sure I'm doing it wrong!!!
    > I get back some rows from the database,


    If it's worth telling us in English, it's worth telling is in Perl.
    (If we need to know they came from a database, show us that you get them
    from the database. If we don't need to know, we don't need to know!)

    > and then want to put those values
    > in checkboxes that if selected, will be then be used to select specific
    > rows from another table.
    > The first line works with the variables, but the checkbox line does not!!
    > I tried quoting and unquoting and requoting etc etc!!!
    > Where am I going wrong???


    When you say it does not work, what do you mean? It doesn't compile? It
    throws a runtime error? Your computer catches fire?

    >
    > foreach (@documents) {
    > ($doc_id,$doc_name,$score) = $documents[$counter+0], $documents
    > [$counter+1], $documents[$counter+2];


    You seem to be incoherently mixing an iterator "for" loop with a index
    method. You didn't show us how/with what @documents is populated, so I
    can't tell you how to do it right, but you are surely doing it wrong.

    > print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";
    > # PREVIOUS LINE WORKS
    > print $q->checkbox (-name =>$doc_name,-value => "YES", -label =>
    > $doc_name);


    This line seems to do exactly what it is supposed to do.

    > # NOT LINE ABOVE
    > $counter ++;


    Whatever @documents is, it is playing role-playing games. Any given element
    will sometimes by a doc_id, sometimes a doc_name, sometimes a score.
    > }


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service New Rate! $9.95/Month 50GB
     
    , Jan 29, 2004
    #2
    1. Advertising

  3. Big Swifty

    cp Guest

    In article <Xns947F6B850158Ckburdishoptonlinenet@167.206.3.2>, Big
    Swifty <> wrote:

    > sure I'm doing it wrong!!!
    > I get back some rows from the database, and then want to put those values
    > in checkboxes that if selected, will be then be used to select specific
    > rows from another table.
    > The first line works with the variables, but the checkbox line does not!!


    Do you go to the doctor and mutter, "I'm not feeling so well?" And
    then, not give enough specifics for a diagnosis?

    Define 'does not work'? What did you expect it to do. What did it do
    instead? Did it produce an error or warning ?

    > I tried quoting and unquoting and requoting etc etc!!!
    > Where am I going wrong???
    >


    >
    > foreach (@documents) {
    > ($doc_id,$doc_name,$score) = $documents[$counter+0], $documents
    > [$counter+1], $documents[$counter+2];


    you're looping through each item in an array. Then you're using the
    first three elements of that array each iteration, offest by one each
    time you loop through. Not, I think, what you have in mind. And I'm not
    sure why you're using those variables anyway. If all your going to do
    is assign it to values of a CGI fucntion call?

    What is actually in @documents? If it is the result of a database
    query, is it perhaps an array of arrayrefs?

    Generally, if you have the results of a database query in an array of
    array refs, you would want a loop lik :

    # untested
    foreach my $row ( @documents ) {
    print qq(<PRE><TT><B>$row->[0] $row->[1] $row->[2]</B></TT></PRE>),
    $q->checkbox(-name=>$row->[0],-value=>'YES',
    -label=>$row->[0] );
    }

    or a more idiomatic version, eliminating the array in the first place
    # prepare and execute a query,
    my $sth = $dbh->prepare( $some_sql_statement );
    $sth->execute();

    # more untested code
    my $row;
    while ( $row = $sth->fetchrow_arrayref() ) {
    print qq(<PRE><TT><B>$row->[0] $row->[1] $row->[2]</B></TT></PRE>),
    $q->checkbox(-name=>$row->[0],-value=>'YES',
    -label=>$row->[0] );
    }


    > print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";
    > # PREVIOUS LINE WORKS


    Define 'works'
    > print $q->checkbox (-name =>$doc_name,-value => "YES", -label =>
    > $doc_name);
    > # NOT LINE ABOVE


    define 'Not line above'

    > $counter ++;
    > }


    --
    cp
     
    cp, Jan 29, 2004
    #3
  4. Big Swifty

    Big Swifty Guest

    Big Swifty <> wrote in
    news:Xns947F6B850158Ckburdishoptonlinenet@167.206.3.2:

    > I can't seem to find any examples that look like what I'm doing, so
    > I'm sure I'm doing it wrong!!!
    > I get back some rows from the database, and then want to put those
    > values in checkboxes that if selected, will be then be used to select
    > specific rows from another table.
    > The first line works with the variables, but the checkbox line does
    > not!! I tried quoting and unquoting and requoting etc etc!!!
    > Where am I going wrong???
    >
    >
    > THANKS IN ADVANCE!!!
    >
    >
    > BIG SWIFTY
    >
    >
    > ........
    >
    > foreach (@documents) {
    > ($doc_id,$doc_name,$score) = $documents[$counter+0], $documents
    > [$counter+1], $documents[$counter+2];
    > print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";
    > # PREVIOUS LINE WORKS
    > print $q->checkbox (-name =>$doc_name,-value => "YES", -label =>
    > $doc_name);
    > # NOT LINE ABOVE
    > $counter ++;
    > }


    Sorry, I should know better, it's out of frustration, and embarassment at
    being new at Perl, Oracle, HTML and CGI!!!!! The code is full of
    commented tried this and that stuff VERY UGLY so I just included what I
    thought was relevant!!

    Thanks for trying to answer my vague question anyway!!!


    I have this CGI script, which I am retrieving values from an Oracle
    database. These values are put into @documents which I am then looping
    through to extract 3 values for each row; doc_id, doc_name and score.
    The problem is I don't know how to handle the variables in the html.
    I see a checkbox but no label. If I look at the source the checkbox name
    is "". I tried quoting $doc_name, unquoting etc etc.

    yet in the line

    print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";

    the value for the variable prints correctly.

    I will go back now and look at your suggestions,

    In the meantime :


    (this seems to be working based on the line that does print correctly)
    sub select {
    undef @documents;
    $sth = $dbh->prepare("SELECT doc_id,doc_name, SCORE(1) score FROM
    mydocuments WHERE CONTAINS(document, ?, 1) > 0
    order by score desc")
    or die "Can't prepare SQL statement: ", $dbh->errstr(), "\n";
    $sth->execute($form_vars{'01_text'}) or die "Can't execute
    SQL statement: ", $sth->errstr(), "\n";

    while (@row = $sth->fetchrow_array ) {
    push (@documents,"$row[0] $row[1] $row[2] ");
    }
    return @documents;
    }


    Then I start generating HTML



    print $q->header ( "text/html"),
    $q->start_html( -title => "Thank you", -bgcolor => "#CCFFCC"),
    $q->h2("Thank you" ),
    $q->b( "$timestamp" ),
    $q->p(),
    $q->b("Your search was for: $form_vars{'01_text'}"),
    $q->hr;


    Then this:

    > foreach (@documents) {
    > ($doc_id,$doc_name,$score) = $documents[$counter+0], $documents
    > [$counter+1], $documents[$counter+2];
    > print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";
    > # PREVIOUS LINE WORKS
    > print $q->checkbox (-name =>$doc_name,-value => "YES", -label =>
    > $doc_name);
    > # NOT LINE ABOVE
    > $counter ++;
    > }



    Then:
    print <<"EOF";
    <P>Return to
    <A HREF="$site_url">$site_name</A>.</P>
    <!-- comment -->
    </BODY>
    </HTML>
    EOF
     
    Big Swifty, Jan 29, 2004
    #4
  5. Big Swifty

    Big Swifty Guest

    Hi Xho:

    Can you tell me what you mean by this?


    You seem to be incoherently mixing an iterator "for" loop with a index
    > method. You didn't show us how/with what @documents is populated, so
    > I can't tell you how to do it right, but you are surely doing it
    > wrong.


    I sent back a reply to my 1st message showing the code to retrieve
    3 columns n rows of data from Oracle db, places into @documents. I use
    the counter to pull back rows and the variables to store the columns.

    Any suggestion on how to do this better would definitely be appreciated!!



    foreach (@documents) {
    ($doc_id,$doc_name,$score) = $documents[$counter+0],
    $documents [$counter+1], $documents[$counter+2];

    print "<PRE><TT><B>$doc_id $doc_name
    >> $score</B></TT></PRE>";
    >> # PREVIOUS LINE WORKS
    >> print $q->checkbox (-name =>$doc_name,-value => "YES", -label
    >> =>
    >> $doc_name);



    Thanks!!!
     
    Big Swifty, Jan 29, 2004
    #5
  6. Big Swifty

    Big Swifty Guest

    Hi CP:

    Please see apologies in other posts!!!

    >
    > you're looping through each item in an array. Then you're using the
    > first three elements of that array each iteration, offest by one each
    > time you loop through. Not, I think, what you have in mind. And I'm
    > not sure why you're using those variables anyway. If all your going to
    > do is assign it to values of a CGI fucntion call?
    >


    I am trying to put the results of an SQL query doc_id, doc_name, score
    into a checkbox list that I will let user check if they want to see the
    doc.


    > What is actually in @documents? If it is the result of a database
    > query, is it perhaps an array of arrayrefs?


    (this seems to be working based on the line that does print correctly)
    sub select {
    undef @documents;
    $sth = $dbh->prepare("SELECT doc_id,doc_name, SCORE(1) score FROM
    mydocuments WHERE CONTAINS(document, ?, 1) > 0
    order by score desc")
    or die "Can't prepare SQL statement: ", $dbh->errstr(), "\n";
    $sth->execute($form_vars{'01_text'}) or die "Can't execute
    SQL statement: ", $sth->errstr(), "\n";

    while (@row = $sth->fetchrow_array ) {
    push (@documents,"$row[0] $row[1] $row[2] ");
    }
    return @documents;
    }





    >
    > Generally, if you have the results of a database query in an array of
    > array refs, you would want a loop lik :
    >


    Since I am new to virtually all of this (Oracle, HTML,CGI, Perl) I try
    taking it step by step so I know where things are going wrong. I realize
    I can combine the database access with generating the HTML, but for now I
    would like to keep it separate. (also in case I want to do something else
    with the returned data)




    > # untested
    > foreach my $row ( @documents ) {
    > print qq(<PRE><TT><B>$row->[0] $row->[1]
    > $row->[2]</B></TT></PRE>),
    > $q->checkbox(-name=>$row->[0],-value=>'YES',
    > -label=>$row->[0] );
    > }
    >
    > or a more idiomatic version, eliminating the array in the first place
    > # prepare and execute a query,
    > my $sth = $dbh->prepare( $some_sql_statement );
    > $sth->execute();
    >
    > # more untested code
    > my $row;
    > while ( $row = $sth->fetchrow_arrayref() ) {
    > print qq(<PRE><TT><B>$row->[0] $row->[1]
    > $row->[2]</B></TT></PRE>),
    > $q->checkbox(-name=>$row->[0],-value=>'YES',
    > -label=>$row->[0] );
    > }
    >
    >
    >> print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";
    >> # PREVIOUS LINE WORKS

    >
    > Define 'works'
    >> print $q->checkbox (-name =>$doc_name,-value => "YES", -label =>
    >> $doc_name);
    >> # NOT LINE ABOVE

    >
    > define 'Not line above'
    >
    >> $counter ++;
    >> }

    >
     
    Big Swifty, Jan 29, 2004
    #6
  7. Big Swifty <> wrote:
    > while (@row = $sth->fetchrow_array ) {
    > push (@documents,"$row[0] $row[1] $row[2] ");
    > }

    [...]
    > > foreach (@documents) {
    > > ($doc_id,$doc_name,$score) = $documents[$counter+0], $documents
    > > [$counter+1], $documents[$counter+2];



    You probably want:
    while (@row = $sth->fetchrow_array) {
    push @documents, [@row];
    }
    ...
    foreach my $rowref (@documents) {
    my ($doc_id,$doc_name,$score) = @$rowref;
    ...


    --
    Glenn Jackman
    NCF Sysadmin
     
    Glenn Jackman, Jan 29, 2004
    #7
  8. Big Swifty

    gnari Guest

    "Big Swifty" <> wrote in message
    news:Xns947F89B28740Bkburdishoptonlinenet@167.206.3.3...
    > Hi Xho:
    >
    > Can you tell me what you mean by this?
    >
    >
    > You seem to be incoherently mixing an iterator "for" loop with a index
    > > method. You didn't show us how/with what @documents is populated, so
    > > I can't tell you how to do it right, but you are surely doing it
    > > wrong.


    > foreach (@documents) {


    here you are iterating through @documents, each pass one
    element of @documents is in $_

    > ($doc_id,$doc_name,$score) = $documents[$counter+0],
    > $documents [$counter+1], $documents[$counter+2];


    but here you are refering to $counter as if it is a loop index
    very confusing

    and what's more quoting here from another post from you in this thread:

    > while (@row = $sth->fetchrow_array ) {
    > push (@documents,"$row[0] $row[1] $row[2] ");
    > }


    @documents seems to contain a set of strings concatenated from
    your table columns

    you MIGHT have meant to do something like:
    while (@row = $sth->fetchrow_array ) {
    push @documents, \@row;
    }

    and then :
    foreach my $row (@documents) {
    ($doc_id,$doc_name,$score) = @$row;
    }

    but who knows?

    gnari
     
    gnari, Jan 29, 2004
    #8
  9. Big Swifty

    G Klinedinst Guest

    Big Swifty <> wrote:

    > foreach (@documents) {
    > ($doc_id,$doc_name,$score) = $documents[$counter+0], $documents
    > [$counter+1], $documents[$counter+2];
    > print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";
    > # PREVIOUS LINE WORKS
    > print $q->checkbox (-name =>$doc_name,-value => "YES", -label =>
    > $doc_name);
    > # NOT LINE ABOVE
    > $counter ++;
    > }


    First can you show us how you are populating @documents(the code).
    Also can you show us the code where you are initializing $q? Is
    @documents a multi-dimensional array?

    My first question is why you are iterating thru @documents, keeping a
    counter, and then using $counter, $counter+1 andn $counter+2 to refer
    to elements of the array? It would seem like if you had an array of
    the numbers 1-10 printed out what you are doing you would get:
    1 2 3
    2 3 4
    3 4 5
    4 5 6
    etc.

    I can't imaging that is what you are trying to do. I'll go out on a
    limb here and say you probably need to be looping through a record set
    rather than an array. If you post more details I imagine someone here
    can help you. I'll certainly try although I am not too familiar with
    the Perl HTML objects.

    -Greg
     
    G Klinedinst, Jan 29, 2004
    #9
  10. Big Swifty

    Guest

    Big Swifty <> wrote:

    > >
    > > foreach (@documents) {
    > > ($doc_id,$doc_name,$score) = $documents[$counter+0], $documents
    > > [$counter+1], $documents[$counter+2];
    > > print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";
    > > # PREVIOUS LINE WORKS
    > > print $q->checkbox (-name =>$doc_name,-value => "YES", -label =>
    > > $doc_name);
    > > # NOT LINE ABOVE
    > > $counter ++;
    > > }


    Ok, I think I see now. You think the print works because $doc_id contains
    all three values that you want, and the other two are empty (or contain
    data for the next things, I'm not sure which). If you used strict and
    warnings, Perl would probably explain this to you.


    > Sorry, I should know better, it's out of frustration, and embarassment at
    > being new at Perl, Oracle, HTML and CGI!!!!! The code is full of
    > commented tried this and that stuff VERY UGLY so I just included what I
    > thought was relevant!!
    >
    > Thanks for trying to answer my vague question anyway!!!
    >
    > I have this CGI script, which I am retrieving values from an Oracle
    > database. These values are put into @documents which I am then looping
    > through to extract 3 values for each row; doc_id, doc_name and score.
    > The problem is I don't know how to handle the variables in the html.


    Nope, the problem occurs before that.

    > I see a checkbox but no label. If I look at the source the checkbox name
    > is "". I tried quoting $doc_name, unquoting etc etc.
    >
    > yet in the line
    >
    > print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";
    >
    > the value for the variable prints correctly.


    Not really. Change that line to:

    print
    "<PRE><TT><B>|||||$doc_id|||||||$doc_name||||$score||||</B></TT></PRE>";

    And see what get's printed.


    > sub select {
    > undef @documents;


    don't do that. use strict and my.

    > $sth = $dbh->prepare("SELECT doc_id,doc_name, SCORE(1) score FROM
    > mydocuments WHERE CONTAINS(document, ?, 1) > 0
    > order by score desc")
    > or die "Can't prepare SQL statement: ", $dbh->errstr(), "\n";
    > $sth->execute($form_vars{'01_text'}) or die "Can't
    > execute SQL statement: ", $sth->errstr(), "\n";
    >
    > while (@row = $sth->fetchrow_array ) {
    > push (@documents,"$row[0] $row[1] $row[2] ");


    What do you think this is doing? Is it adding one thing to @documents,
    or is it adding 3 things to @documents?

    > }
    > return @documents;
    > }



    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service New Rate! $9.95/Month 50GB
     
    , Jan 29, 2004
    #10
  11. Glenn Jackman <> wrote:

    > Big Swifty <> wrote:
    >> while (@row = $sth->fetchrow_array ) {
    >> push (@documents,"$row[0] $row[1] $row[2] ");
    >> }

    > [...]
    >> > foreach (@documents) {
    >> > ($doc_id,$doc_name,$score) = $documents[$counter+0], $documents
    >> > [$counter+1], $documents[$counter+2];

    >
    >
    > You probably want:
    > while (@row = $sth->fetchrow_array) {
    > push @documents, [@row];
    > }
    > ...
    > foreach my $rowref (@documents) {
    > my ($doc_id,$doc_name,$score) = @$rowref;
    > ...


    "Big Swifty" is going to be building some simple data structures, so s/he
    would also benefit from reading the following:

    perldoc perllol
    perldoc perldsc

    and also

    perldoc perlref

    for more general info about references.

    I also noticed that the code doesn't seem to have strictures and warnings
    enabled, and looks as if it depends on global variables.

    To "Big Swifty":

    use strict;
    use warnings;

    should be at the beginning of all non-trivial programs (unless you have a
    really good reason and know exactly why you're not enabling them).
    Consider them essential safety equipment for detecting typos and other
    careless mistakes.

    For info about passing data into subroutines instead of using global
    variables, see

    perldoc perlsub

    --
    David Wall
     
    David K. Wall, Jan 29, 2004
    #11
  12. Big Swifty

    Guest

    Big Swifty <> wrote:
    > Hi Xho:
    >
    > Can you tell me what you mean by this?
    >
    > You seem to be incoherently mixing an iterator "for" loop with a index
    > > method. You didn't show us how/with what @documents is populated, so
    > > I can't tell you how to do it right, but you are surely doing it
    > > wrong.


    I'm not sure how else to explain it. when you use the construct

    foreach (@documents) {

    you are telling to to set $_ to each member of @documents in turn,
    and run the body of the loop. You should be working with the contents of
    $_, not explicitly with the elements @documents.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service New Rate! $9.95/Month 50GB
     
    , Jan 29, 2004
    #12
  13. Big Swifty

    Big Swifty Guest

    Glenn Jackman <> wrote in
    news::

    > You probably want:
    > while (@row = $sth->fetchrow_array) {
    > push @documents, [@row];
    > }
    > ...
    > foreach my $rowref (@documents) {
    > my ($doc_id,$doc_name,$score) = @$rowref;
    > ...
    >


    Glenn:

    This worked!! I don't understand yet why, What do the brackets do in
    [@row]
    You are pushing the @row array on top of the @documents array. And then
    dereferencing with @$rowref???



    I'm still not sure why I was able to get values for all 3 variables in
    this line but couldn't get $doc_name in next


    print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";
    print $q->checkbox (-name =>$doc_name,-value => "YES", -label =>>
    $doc_name);

    On to the next ball buster!!


    Thanks a mil to you and all that responded!!!
     
    Big Swifty, Jan 29, 2004
    #13
  14. Big Swifty

    Big Swifty Guest

    "David K. Wall" <> wrote in
    news:Xns947F94DC06244dkwwashere@216.168.3.30:

    > "Big Swifty" is going to be building some simple data structures, so
    > s/he would also benefit from reading the following:
    >
    > perldoc perllol
    > perldoc perldsc
    >
    > and also
    >
    > perldoc perlref
    >
    > for more general info about references.
    >
    > I also noticed that the code doesn't seem to have strictures and
    > warnings enabled, and looks as if it depends on global variables.
    >
    > To "Big Swifty":
    >
    > use strict;
    > use warnings;
    >
    > should be at the beginning of all non-trivial programs (unless you
    > have a really good reason and know exactly why you're not enabling
    > them). Consider them essential safety equipment for detecting typos
    > and other careless mistakes.
    >
    > For info about passing data into subroutines instead of using global
    > variables, see
    >
    > perldoc perlsub
    >


    All VERY helpful information, I know I am bypassing a lot of security, good
    coding practices etc. I'm trying to get the stuff to just work first and
    I'll go back and try and improve later!!
    BTW I would consider anything I've been able to do so far trivial!! :)
    Thanks David!
     
    Big Swifty, Jan 29, 2004
    #14
  15. Big Swifty

    Big Swifty Guest

    wrote in news:20040129135950.858$:

    > Big Swifty <> wrote:
    >
    >> >
    >> > foreach (@documents) {
    >> > ($doc_id,$doc_name,$score) = $documents[$counter+0],
    >> > $documents [$counter+1], $documents[$counter+2];
    >> > print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";
    >> > # PREVIOUS LINE WORKS
    >> > print $q->checkbox (-name =>$doc_name,-value => "YES", -label
    >> > =>
    >> > $doc_name);
    >> > # NOT LINE ABOVE
    >> > $counter ++;
    >> > }

    >
    > Ok, I think I see now. You think the print works because $doc_id
    > contains all three values that you want, and the other two are empty
    > (or contain data for the next things, I'm not sure which). If you
    > used strict and warnings, Perl would probably explain this to you.


    I thought the first print works because I would get 4 rows of discrete
    column info, $doc_id, $doc_name, $score
    but for each checkbox couldn't see label, and view source showed empty
    string for the name.

    >
    > Nope, the problem occurs before that.


    Ok, I recognize it started in childhood!!! :)

    >
    >> I see a checkbox but no label. If I look at the source the checkbox
    >> name is "". I tried quoting $doc_name, unquoting etc etc.
    >>
    >> yet in the line
    >>
    >> print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";
    >>
    >> the value for the variable prints correctly.

    >
    > Not really. Change that line to:
    >
    > print
    > "<PRE><TT><B>|||||$doc_id|||||||$doc_name||||$score||||</B></TT></PRE>"
    > ;
    >
    > And see what get's printed.



    |||||3|||||||learn_oracle3.txt||||87||||
    |||||4|||||||learn_oracle4.txt||||3||||
    |||||2|||||||learn_oracle2.txt||||3||||
    |||||1|||||||learn_oracle.txt||||3||||


    I'm not seeing your point...



    >
    >> sub select {
    >> undef @documents;

    >
    > don't do that. use strict and my.


    OK POINT TAKEN


    >
    >> $sth = $dbh->prepare("SELECT doc_id,doc_name, SCORE(1) score
    >> FROM mydocuments WHERE CONTAINS(document, ?, 1) > 0
    >> order by score desc")
    >> or die "Can't prepare SQL statement: ", $dbh->errstr(), "\n";
    >> $sth->execute($form_vars{'01_text'}) or die "Can't
    >> execute SQL statement: ", $sth->errstr(), "\n";
    >>
    >> while (@row = $sth->fetchrow_array ) {
    >> push (@documents,"$row[0] $row[1] $row[2] ");

    >
    > What do you think this is doing? Is it adding one thing to
    > @documents, or is it adding 3 things to @documents?
    >
    >> }
    >> return @documents;
    >> }

    >
    >
    > Xho
    >
     
    Big Swifty, Jan 29, 2004
    #15
  16. Big Swifty

    Big Swifty Guest

    Greg:
    Please see other posts for the code, and apologies for vague question.


    >
    > My first question is why you are iterating thru @documents, keeping a
    > counter, and then using $counter, $counter+1 andn $counter+2 to refer
    > to elements of the array? It would seem like if you had an array of
    > the numbers 1-10 printed out what you are doing you would get:
    > 1 2 3
    > 2 3 4
    > 3 4 5
    > 4 5 6
    > etc.

    foreach (@documents) {
    > > ($doc_id,$doc_name,$score) = $documents[$counter+0], $documents
    > > [$counter+1], $documents[$counter+2];


    I was getting the results I wanted with this line
    print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";

    I was trying to get @documents to act like a multi-dimensional array
    3 columns and n rows are retrieved from the database, and it looked like it
    was working, is it really that weird????




    >
    > I can't imaging that is what you are trying to do. I'll go out on a
    > limb here and say you probably need to be looping through a record set
    > rather than an array. If you post more details I imagine someone here
    > can help you. I'll certainly try although I am not too familiar with
    > the Perl HTML objects.


    Yea that sounds like what I was trying to do (record set)

    >
    > -Greg
    >
     
    Big Swifty, Jan 29, 2004
    #16
  17. Big Swifty <> wrote:

    > Glenn Jackman <> wrote in
    > news::
    >
    >> You probably want:
    >> while (@row = $sth->fetchrow_array) {
    >> push @documents, [@row];
    >> }
    >> ...
    >> foreach my $rowref (@documents) {
    >> my ($doc_id,$doc_name,$score) = @$rowref;
    >> ...
    >>

    >
    > Glenn:
    >
    > This worked!! I don't understand yet why, What do the brackets do in
    > [@row]


    Since @row appears to be defined outside the while() loop, push()ing
    references to @row would yield results you probably wouldn't expect. That
    is, Glenn deliberately avoided doing this:

    push @documents, \@row;


    > You are pushing the @row array on top of the @documents array. And then
    > dereferencing with @$rowref???


    Not quite. The square brackets create an anonymous array, which @row then
    populates. These anonymous array references are pushed onto (into?)
    @documents, because arrays can only contain scalars (such as references),
    not other arrays. So Glenn was dereferencing the references to anonymous
    arrays when he used @$rowref. (This paragraph should be taken out and
    shot)

    > I'm still not sure why I was able to get values for all 3 variables in
    > this line but couldn't get $doc_name in next
    >
    >
    > print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";
    > print $q->checkbox (-name =>$doc_name,-value => "YES", -label =>>
    > $doc_name);


    Is that your actual code? There's a space after checkbox and an extra >,
    so it won't even compile.

    --
    David Wall
     
    David K. Wall, Jan 29, 2004
    #17
  18. Big Swifty

    gnari Guest

    "Big Swifty" <> wrote in message
    news:Xns947F9FB7A206kburdishoptonlinenet@167.206.3.2...
    > wrote in news:20040129135950.858$:
    >
    > > Big Swifty <> wrote:
    > >
    > >> yet in the line
    > >>
    > >> print "<PRE><TT><B>$doc_id $doc_name $score</B></TT></PRE>";
    > >>
    > >> the value for the variable prints correctly.

    > >
    > > Not really. Change that line to:
    > >
    > > print
    > > "<PRE><TT><B>|||||$doc_id|||||||$doc_name||||$score||||</B></TT></PRE>"
    > > ;
    > >
    > > And see what get's printed.

    >
    >
    > |||||3|||||||learn_oracle3.txt||||87||||
    > |||||4|||||||learn_oracle4.txt||||3||||
    > |||||2|||||||learn_oracle2.txt||||3||||
    > |||||1|||||||learn_oracle.txt||||3||||
    >
    >
    > I'm not seeing your point...


    did you make the program print it or did you just write
    what you assumed would be printed?

    because see the following snippet:

    > >> while (@row = $sth->fetchrow_array ) {
    > >> push (@documents,"$row[0] $row[1] $row[2] ");


    see you are adding one string only, that happen to contain the 3 values,
    so the
    print
    "<PRE><TT><B>|||||$doc_id|||||||$doc_name||||$score||||</B></TT></PRE>";
    above should have printed:
    |||||3 learn_oracle3.txt 87 |||||||||||||||

    gnari
     
    gnari, Jan 29, 2004
    #18
  19. Big Swifty

    Big Swifty Guest

    "David K. Wall" <> wrote in
    news:Xns947FAE44C390Adkwwashere@216.168.3.30:

    >> This worked!! I don't understand yet why, What do the brackets do in
    >> [@row]

    >
    > Since @row appears to be defined outside the while() loop, push()ing
    > references to @row would yield results you probably wouldn't expect.
    > That is, Glenn deliberately avoided doing this:
    >
    > push @documents, \@row;
    >
    >
    >> You are pushing the @row array on top of the @documents array. And
    >> then dereferencing with @$rowref???

    >
    > Not quite. The square brackets create an anonymous array, which @row
    > then populates. These anonymous array references are pushed onto
    > (into?) @documents, because arrays can only contain scalars (such as
    > references), not other arrays. So Glenn was dereferencing the
    > references to anonymous arrays when he used @$rowref. (This paragraph
    > should be taken out and shot)

    THANKS for the explanation!

    print $q->checkbox (-name =>$doc_name,-value => "YES", -label =>
    $doc_name);
    >
    > Is that your actual code? There's a space after checkbox and an extra
    > >, so it won't even compile.

    >


    I think the > got added from copy/paste but it seems that it works with
    checkbox and space.
     
    Big Swifty, Jan 29, 2004
    #19
  20. Big Swifty

    Thomas Kratz Guest

    Glenn Jackman wrote:
    > Big Swifty <> wrote:
    >
    >> while (@row = $sth->fetchrow_array ) {
    >> push (@documents,"$row[0] $row[1] $row[2] ");
    >> }

    >
    > [...]
    >
    >>>foreach (@documents) {
    >>> ($doc_id,$doc_name,$score) = $documents[$counter+0], $documents
    >>> [$counter+1], $documents[$counter+2];

    >
    >
    >
    > You probably want:
    > while (@row = $sth->fetchrow_array) {
    > push @documents, [@row];
    > }
    > ...
    > foreach my $rowref (@documents) {
    > my ($doc_id,$doc_name,$score) = @$rowref;
    > ...


    Or even better because you can access the values by name nad DBI does
    everything for you as long as the columns are named:

    sub select {

    ...

    my $docs = $sth->fetchall_arrayref({});

    return($docs);
    }

    and later

    my $selected = select();

    foreach my $doc ( @$selected ) {
    ... do something with $doc->{doc_id}, $doc->{doc_name} and
    $doc->{score}
    }

    It looks so much clearer, than referring to the result by column number,
    although it has to be somewhat slower (never benchmarked this, never mattered)

    Thomas

    --
    open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
    STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
    print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</ #.>s^~h<t< ..~. ...c.^..
    &&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#....>>e>r^..>l^...>k^..
     
    Thomas Kratz, Jan 30, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmlnYWw=?=

    Can someone tell me what is wrong here?

    =?Utf-8?B?QmlnYWw=?=, Aug 11, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    623
    =?Utf-8?B?QmlnYWw=?=
    Aug 12, 2005
  2. marco
    Replies:
    2
    Views:
    372
    marco
    Sep 2, 2004
  3. Dildo Boy
    Replies:
    6
    Views:
    343
    Flash Gordon
    Mar 6, 2007
  4. E-Dot
    Replies:
    15
    Views:
    550
  5. Cathryn Lindner

    Can someone tell me what I'm doing wrong?

    Cathryn Lindner, Nov 4, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    133
    Cathryn Lindner
    Nov 4, 2003
Loading...

Share This Page