in reply to use simple scalar names to point to specific array elements
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re:^2 use simple scalar names to point to specific array elements
by Roy Johnson (Monsignor) on Dec 05, 2003 at 23:04 UTC | |
by Sandy (Curate) on Dec 05, 2003 at 23:29 UTC |