Solved

Quick/East perl question

Posted on 2003-11-25
11
566 Views
Last Modified: 2008-03-03
There are two sql statements/storedprocs. When executed, they return those two resultsets

sql1:  column1 column2 column3 column4
sql2:  column5 column3  
note: column3 is the same column for sql1 and sql2

 
in perl,

I have the code to get resultset from db as:

for sql1 query:

...
my (@lines, @data);
...
while( $databasehandle->dbresults != NO_MORE_RESULTS)
{
         $delimiter = "\t";
          for (;;)
         {
             ..........
             @data = $databasehandle->dbnextrow;
             push(@lines, join("$delimiter", @data));
            .........
         }
}
....
foreach $str (@lines)                 
{
        ($perlcol1, $perlcol2, $perlcol3, $perlcol4) = split /\t/, $str;
}

So this maps column1->$perlcol1 ...etc  column4->$perlcol4

Now i have the exact same code to run sql2 query except

foreach $str (@arr)
{

            ($perlcol5, $perlcol1) = split /\t/, $str;
}

this maps column5->$perlcol5,  and column3 -> $perlcol1

Note the difference between sql1 and sql2's result mapping
column1->$perlcol1
column3->$perlcol1

This code has been in production for a while and works fine so i assume it is correct, however it just doesnt make sense and seems like a bug. The program then uses $perlcol1 and $perlcol3 in a lot of hash tables etc.

For example, there is a hashtable defined as hashname{column1}, then it does
hashname{$perlcol1} to use it. EXCEPT now $perlcol1 points to column3 not column1. I dont see how they could possiblly map to the correct values. Am i right, or is it some perl syntax i am missing.

Thank you,

gaga
0
Comment
Question by:gagaliya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 20

Expert Comment

by:jmcg
ID: 9819895
As you've described it so far, it does appear that there's an opportunity for a great deal of confusion. What you have not shown us may hold the key to why things manage to work despite the confusion. If the hash is always indexed by $perlcol1 when using a value of $perlcol1 derived from splitting the sql2 query results and is always indexed by a $perlcol3 derived from splitting the sql1 query results, then that would appear to let things work as intended.

I doubt very much that there is any strange Perl magic going on here.
0
 
LVL 3

Expert Comment

by:terageek
ID: 9819983
I would guess that sql2 is actually column5 column1, and not column5 column3.  This would make a lot more sense than mapping column3 to a vairaible called $perlcol1.
0
 
LVL 1

Author Comment

by:gagaliya
ID: 9821044
"I would guess that sql2 is actually column5 column1, and not column5 column3.  This would make a lot more sense than mapping column3 to a vairaible called $perlcol1. "

that's exactly where the problem is. If sql2 is col5 col1 then everything makes perfect sense. But it is col5 col3 which is where the confusion begins.

jmcg,  maybe this will help some more.  I added in the comments(are they correct?), but the code is 100% correct.

@arr = doSql(); //@arr is sql1's result -  column1 column2 column3 column4

foreach $str (@arr)
{
       ($perlcol1, $perlcol2, $perlcol3, $perlcol4) = split /\t/, $str;
       $_hash2{$perlcol1} = $perlcol4;  //here hash2 is keyed with $perlcol1(which is column1) data
}
@arr = doSql();  //@arr is sql2's result - column5 column3
undef %_hash4;

foreach $str (@arr)
{
     ($perlcol5, $perlcol1) = split /\t/, $str;  //here $perlcol1 becomes column3 data instead of column1 data
      $_hash3{$perlcol5} = $perlcol1;
      $_hash4{$perlcol5} = $hash2{$perlcol1}; //here it's trying to use column3 data as the key for hash2 when the key in hash2 is data from column1!
}


Amazing thing is it is working, but why? i am so confused.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 20

Expert Comment

by:jmcg
ID: 9821284
Sorry, this makes very little sense to me. Is $hash2 a typo intended to be $_hash2?
0
 
LVL 3

Expert Comment

by:terageek
ID: 9821489
Does col1 and col3 contain similar types of data?  Can you give an example of the data set?
0
 
LVL 1

Author Comment

by:gagaliya
ID: 9824994
jmcg, it is not a typo. Whoever wrote the code used $_hash2 first then used $hash2 exactly like in th code i provided. I thought they mean the same thing?

terageek, col1 is a string which contains abbreviations, and col3 is the actual long name. For example col1: CA  col3 Computer Associates.  But sometime they might be the same, example col1: IBM, col3: IBM.

thanks,

gaga
0
 
LVL 3

Assisted Solution

by:terageek
terageek earned 25 total points
ID: 9825493
I think you 1/2 answered your own question there.  %_hash2 and %hash2 are not the same thing.  %hash2 could key off long names, where %_hash2 keys off of short names.  You can try to take a look at the keys of both hashes to check it out.
0
 
LVL 1

Author Comment

by:gagaliya
ID: 9825555
but if you look at   $_hash4{$perlcol5} = $hash2{$perlcol1};
if $hash2 is not the same as $_hash2, then $hash2 was never defined/populated with data. So why is it trying to get value from it.
0
 
LVL 20

Assisted Solution

by:jmcg
jmcg earned 100 total points
ID: 9826480
If there is a unique bi-directional mapping between col1 and col3, then either one can be used as a unique key for accessing the hash. But it still has to be done with some consistency.
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 125 total points
ID: 9830367
> column1->$perlcol1
> column3->$perlcol1

as I read your posted code,  these is

   column1->$perlcol1
   column3->$perlcol3

for your sql1, and for sql2 it is

   column5->$perlcol5
   column3->$perlcol1

Does this make sense?
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10093665
Nothing has happened on this question in more than 6 weeks. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points [grade B] between jmcg [100 pts], terageek [25 pts], and ahoffman [125 pts]. (abandoned by asker, not completely resolved).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question