Link to home
Start Free TrialLog in
Avatar of gagaliya
gagaliya

asked on

Quick/East perl question

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
Avatar of jmcg
jmcg
Flag of United States of America image

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.
Avatar of terageek
terageek

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.
Avatar of gagaliya

ASKER

"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.
Sorry, this makes very little sense to me. Is $hash2 a typo intended to be $_hash2?
Does col1 and col3 contain similar types of data?  Can you give an example of the data set?
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
SOLUTION
Avatar of terageek
terageek

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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