First, if you want to extract and compare two columns, your query needs to select two columns. Your query is only extracting column 1.

Second, looking at your output, it appears that the problem is to follow a chain of match-ups. Thus if you find the pair "apple-bannana" you need to match it up with the pair "bannana-kite" - is that right? You can't just do this by printing out three columns, like you are doing. Instead you are going to need to organize your data to make it easy to take the value in column 2 and search for it in column 1. How to do that?

You will need two steps:

  1. Convert the rows of the table into a hash
  2. When you are done, with the conversion, search the hash for matching pairs and print them out.

A hash is kind of like two array columns - but it lets you search easily on one of the columns. For a tutorial, see Hash Crash Course

This looks a lot like a homework problem, so I'm not going to write out the code for you. Read the tutorial and update your post to reflect what you've learned. If you are having trouble using hashes, I'm sure many monks will be eager to help you if they see some code that shows what you've tried to do. The code makes it easier for us to see what you are stuck on.

Note: it is also possible that that this is not a Perl problem at all. This kind of match up can also be done using an SQL query: you need to alias your table and then join the column 1 of the table to column 2 of the aliased table. Select column 1 and 2 from the table and set column 3 to column 2 of the aliased table. I'll leave the actual construction of the query to you. For a brief tutorial on self joins, see SQL Join- Self-Join

Best, beth

Update: May not be a Perl problem at all


In reply to Re: perl and mysql column by ELISHEVA
in thread perl and mysql column by sharan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.