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

Combining variables to create a single virtual variable

by Freezer (Sexton)
on Sep 21, 2012 at 08:05 UTC ( #994842=perlquestion: print w/replies, xml ) Need Help??

Freezer has asked for the wisdom of the Perl Monks concerning the following question:



Dear Monks,

What is the best way to to combine variables to create one virtual variable? For example with the following piece of code I don't want to have to repeat this statement 4 times because the amout of Perl code generated becomes difficult to manage. If possible I want the code to make it obvious that we are dealing with a fusion of two variable names. So, for example,  $sth_Count_Ref_vs_Comp.$letter_tag Then I just need to change the letter tag within a loop.
my $sth_Count_Ref_vs_Comp = $dbh_B->prepare($sql_count_A_Ref_vs_ +Comp) or die "Cannot prepare: " . $dbh_B->errstr(); $sth_Count_Ref_vs_Comp->execute() or die "$sth_Count_Ref_vs_Comp +->errstr\n"; my $sth_Count_Ref_vs_Comp_B = $dbh_B->prepare($sql_count_A_Ref_v +s_Comp_B) or die "Cannot prepare: " . $dbh_B->errstr(); $sth_Count_Ref_vs_Comp_B->execute() or die "$sth_Count_Ref_vs_Co +mp_B->errstr\n"; my $sth_Count_Ref_vs_Comp_C = $dbh_B->prepare($sql_count_A_Ref_v +s_Comp_C) or die "Cannot prepare: " . $dbh_B->errstr(); $sth_Count_Ref_vs_Comp_C->execute() or die "$sth_Count_Ref_vs_Co +mp_C->errstr\n"; my $sth_Count_Ref_vs_Comp_D = $dbh_B->prepare($sql_count_A_Ref_v +s_Comp_D) or die "Cannot prepare: " . $dbh_B->errstr(); $sth_Count_Ref_vs_Comp_D->execute() or die "$sth_Count_Ref_vs_Co +mp_D->errstr\n";

Replies are listed 'Best First'.
Re: Combining variables to create a single virtual variable
by davido (Cardinal) on Sep 21, 2012 at 08:18 UTC

    Identifier names like $sql_count_A_Ref_vs_Comp_B might be indicative of a deeper design problem. But a solution to the immediate problem presented could be:

    foreach my $sql ( $sql_count_A_Ref_vs_Comp, $sql_count_A_Ref_vs_Comp_B, $sql_count_A_ref_vs_Comp_C, $sql_count_A_Ref_vs_Comp_D ) { my $sth = $dbh_B->prepare( $sql ) or die "Cannot prepare: " . $dbh +_B->errstr(); $sth->execute() or die $sth->errstr; }

    This lets $sql stand in as an alias for each of those sql_count_A_blah_blah_blah variables. Then you just loop over them, repeating the same process for each.


    Dave

      Would it then be possible to incorporate this as well? It might be better if I kept the code all strung out and just tidied up the formating :<
      my @row_A_count_A_Ref_vs_Comp; my @fields_A_count_A_Ref_vs_Comp; my @record_A_count_A_Ref_vs_Comp; print BILATERAL_COMPARISONS_A "$sql_count_A_Ref_vs_Comp"; while(my @row_A_count_A_Ref_vs_Comp = $sth_Count_Ref_vs_Comp->fe +tchrow_array()) { print BILATERAL_COMPARISONS_A "\t\t\t\t"; print BILATERAL_COMPARISONS_A "$entry_no_new_MINUS_ONE\t"; + local $\ = "\n"; local $, = "\t"; print BILATERAL_COMPARISONS_A @row_A_count_A_Ref_vs_Comp; + my @record_A_count_A_Ref_vs_Comp = @row_A_count_A_Ref_vs_Com +p; push(my @fields_A_count_A_Ref_vs_Comp, @record_A_count_A_Ref +_vs_Comp); } $sth_Count_Ref_vs_Comp->finish();
Re: Combining variables to create a single virtual variable
by Athanasius (Archbishop) on Sep 21, 2012 at 08:23 UTC

    I don’t know what you mean by a “virtual variable”, but why not just use hashes:

    my %sql_count_A_Ref_vs_Comp; my %sth_Count_Ref_vs_Comp; for (qw( A B C D )) { $sth_Count_Ref_vs_Comp{$_} = $dbh_B->prepare($sql_count_A_Ref_vs_C +omp{$_}) or die "Cannot prepare: " . $dbh_B->errstr(); $sth_Count_Ref_vs_Comp{$_}->execute() or die "$sth_Count_Ref_vs_Co +mp{$_}->errstr\n"; }

    Adjust as needed to accommodate $letter_tag as the hash key.

    Hope that helps,

    Athanasius <°(((><contra mundum

Re: Combining variables to create a single virtual variable
by sundialsvc4 (Abbot) on Sep 21, 2012 at 12:59 UTC

    Another perhaps even-more-obvious way to present it would be to make one or more correspondings lists of letter-tag names, then iterate through that list making interpolations to build up the SQL string.   (Variable-names only, of course ... from a known fixed list, of course ...)   If there is a natural symmetry that you want to emphasize to the next reader of the source code, which of course is a very desirable thing to do, then I think that this would be a “clean and obvious” way to do that.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://994842]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2022-01-22 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (61 votes). Check out past polls.

    Notices?