To all,

Thank you for your input. It is good to know that an 'alias' is possible, although I don't think I need it for my current problem.

I know it was the question I asked because I had hoped it would solve my problem. Nonetheless, I am happy to learn all new things.

My Problem:

I wanted an easy way to ensure that the order of a list of columns read from database query could be modified, without any mistakes.

I was worried that if someone changed the select statement, they might forget to change the statement that actually reads the data.

My hopeful solution... create a single list of columns that would be used to construct the query statement, and be used to parse the query result.

Condition to this hopeful solution... I still want to use 'use strict' because it will also prevent some muck-ups.

I think I have got it!. See below

#!/usr/bin/perl -w # ------------------------------------------ # PURPOSE: # - Test if I can make a program that reads # a db table. # Maintainability issue: Want to be able to easily # manipulate the order in which the columns # are read. # # To change order of which columns are selected/read # from db only requires changing @parm_cols. # # Robustness? If a column is added to @parm_cols # without predefining the variable, use strict will # complain if one tries to use that variable.? # # ------------------------------------------ use strict; # ------------------ # define my variables # ------------------ my ( $abc, $def, $ghi, $jkl ); my @parm_cols = (); my $map_to_array; # ----------------- # define columns and order to read them # Note order of columns not same as the # my definition above. # ----------------- @parm_cols = qw( abc ghi jkl def ); # --------------------- # define select statement # --------------------- my $select = 'SELECT '.join(",",@parm_cols); # --------------------- # define how to map array which # is result of query statement to db # to individual variables # --------------------- map {$_='$'.$_} (my @tmp = @parm_cols); $map_to_array = join (",",@tmp); # --------------------- # read db (for testing purpose, just set array) # --------------------- my @array = (4,3,2,1); # --------------------- # populate my already declared # variables # --------------------- eval "($map_to_array) = ".'@array'; # --------------------- # test to see if it works # --------------------- print "$abc $def $ghi $jkl\n"; exit;

Thanks again,

Sandy


In reply to Re: use simple scalar names to point to specific array elements by Sandy
in thread use simple scalar names to point to specific array elements by Sandy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.