Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 575
  • Last Modified:

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
0
gagaliya
Asked:
gagaliya
  • 4
  • 3
  • 3
  • +1
3 Solutions
 
jmcgOwnerCommented:
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
 
terageekCommented:
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
 
gagaliyaAuthor Commented:
"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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jmcgOwnerCommented:
Sorry, this makes very little sense to me. Is $hash2 a typo intended to be $_hash2?
0
 
terageekCommented:
Does col1 and col3 contain similar types of data?  Can you give an example of the data set?
0
 
gagaliyaAuthor Commented:
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
 
terageekCommented:
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
 
gagaliyaAuthor Commented:
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
 
jmcgOwnerCommented:
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
 
ahoffmannCommented:
> 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
 
jmcgOwnerCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now