Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^5: mysql DBI Nested Queries

by roboticus (Chancellor)
on Dec 28, 2012 at 15:50 UTC ( #1010714=note: print w/replies, xml ) Need Help??

in reply to Re^4: mysql DBI Nested Queries
in thread mysql DBI Nested Queries


As part of the thinking process, we make assumptions to make the problem simpler. Computers, however, can't make assumptions, so we can get into trouble when translating our solution to a programming language. So, when we program, we have to learn how to be aware of the assumptions we're making, so we can question them. It's pretty difficult, and after some 35 years, I'm still tripping over my own assumptions.

That's just a long-winded way of getting to your question. The assumptions you're tripping over at the moment are in these two statements:

The key challenge is the conversion of the $next_level_supervisor variable. When I approach the problem iteratively, I just change the variable name each time so the variable returned is $supervisor2, $supervisor3. until there are no additional supervisors. (Although I can add a WHILE $supervisorX ne CEO the program seems to close properly even though I iterate through 10 supervisors without that WHILE condition).
How to I differentiate the input $next_level_supervisor from the output $next_level_supervisor? Somehow I have to successfully convert the $next_level_supervisor2 value into the $next_level_supervisor as input to the sub routine. Im stumped.

As I see it there are two assumptions you're making here that are causing you grief: (1) That one supervisor is somehow different from another, and (2) that a supervisor isn't exactly an employee.

In your problem, when you're given an employee, you want to add the employee to the spreadsheet, and then add their supervisor to the spreadsheet, and then their supervisor to the spreadsheet. Since you're treating different supervisors differently from each other, and not taking advantage that a supervisor is also an employee, you're not seeing the simplification.

So, what if one supervisor is the same as any other supervisor? Then you don't really need a different variable for it.

Let's take a different but similar problem: Let's factor a number. The method you're currently using is similar to this:

my ($f1, $f2, $f3, $f4, $f5); my $c = <>; # get number ($f1,$c) = get_a_factor($c); print "$f1, "; ($f2,$c) = get_a_factor($c); print "$f2, "; ($f3,$c) = get_a_factor($c); print "$f3, "; ($f4,$c) = get_a_factor($c); print "$f4, "; ($f5,$c) = get_a_factor($c); print "$f5\n"; sub get_a_factor { my $num = shift; ... compute factor ... return ($factor, $num/$factor); }

The problems here are (1) that we're treating each factor as a different type of thing, and (2) we have to know how many factors we can get.

But once we print the factor, nothing in our code really cares about it any longer. We simply want the list of remaining factors. So we can simplify it like so:

my $f1; my $c = <>; # get number # Get first factor ($f1,$c) = get_a_factor($c); print $f1; # Get rest of 'em while ($c > 1) { ($f1,$c) = get_a_factor($c); print ", $f1"; } print "\n"; sub get_a_factor { my $num = shift; ... compute factor ... return ($factor, $num/$factor); }

As you can see, we no longer have to worry how many factors there may ultimately be, as we can simply get all of them, until the loop terminates.

However, we are still treating the first factor as being somehow a different thing than the others. Since the supervisor number is just another employee ID, you could combine the loop with the first step if you rearrange a little more:

my $f1; my $c = <>; # get number # Get factor and the new number to test while ($c > 1) { ($f1,$c) = get_a_factor($c); print ", $f1"; } print "\n"; sub get_a_factor { my $num = shift; ... compute factor ... return ($factor, $num/$factor); }

Does this give you the hints you need? If not let me know and I'll give you some more.


When your only tool is a hammer, all problems look like your thumb.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1010714]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2018-07-22 14:51 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (454 votes). Check out past polls.