For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > September 2004 > #define-like feature in Perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author #define-like feature in Perl
Yash

2004-09-29, 11:02 am

Hi,

In a perl 5.8 program, We have to use a long list of variables in a
certain order in a number of places. It is something like:
my ($var1, $field2, $a3, $b4, .....) ;
A. Sinan Unur

2004-09-29, 8:05 pm

yashgt@yahoo.com (Yash) wrote in
news:5a373b1d.0409290455.23f5bbee@posting.google.com:

> In a perl 5.8 program, We have to use a long list of variables in a
> certain order in a number of places. It is something like:
> my ($var1, $field2, $a3, $b4, .....) ;
> .
> .
> .
> foreach $x ($var1, $field2, $a3, $b4, .....) {
> $x = -1 ;
> }
> .
> .
> .
> print join(",",($var1, $field2, $a3, $b4, .....)) ;
>
> Instead of having to repeat the names of the fields and their order,
> in all places of use, we would like to have something like
> #define FIELD_LIST $var1, $field2, $a3, $b4, .....
>
> and user FIELD_LIST in all places.
> This is important for us as the fields and their order may change as
> the program evolves.
> Can somebody suggest a better alternative?


Consider the following alternative to the example you gave above:

#! perl

use strict;
use warnings;

my %hash = (var1 => 1, field2 => 2, a3 => 3, b4 => 4);

$_ = -1 for (values %hash);
print join ',', values %hash;

__END__

Sinan
Ron Parker

2004-09-29, 8:05 pm

On 29 Sep 2004 13:07:09 GMT, A. Sinan Unur wrote:
> print join ',', values %hash;


$ perldoc -f values
=item values HASH

Returns a list consisting of all the values of the named hash. (In a
scalar context, returns the number of values.) The values are
returned in an apparently random order.
----

You don't think that "random order" thing might be a problem?

--
#local R=<7084844682857967,0787982,826975826580>;#macro L(P)concat(#while(P)chr(
mod(P,100)),#local P=P/100;#end"")#end background{rgb 1}text{ttf L(R.x)L(R.y)0,0
translate<-.8,0,-1>}text{ttf L(R.x)L(R.z)0,0translate<-1.6,-.75,-1>}sphere{z/9e3
4/26/2001finish{reflection 1}}//ron.parker@povray.org My opinions, nobody else's
Brian Wakem

2004-09-29, 8:05 pm


"A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote in message
news:Xns95735CC3C606Aasu1cornelledu@132.236.56.8...
> yashgt@yahoo.com (Yash) wrote in
> news:5a373b1d.0409290455.23f5bbee@posting.google.com:
>
>
> Consider the following alternative to the example you gave above:
>
> #! perl
>
> use strict;
> use warnings;
>
> my %hash = (var1 => 1, field2 => 2, a3 => 3, b4 => 4);
>
> $_ = -1 for (values %hash);
> print join ',', values %hash;



But this wouldn't necessarily join them in the correct order. It wouldn't
matter if they were all of equal value, but I doubt this is what the OP
wanted.

my @array = (1, 2, 3, 4);
print join ',',@array;

--
Brian Wakem


Gunnar Hjalmarsson

2004-09-29, 8:05 pm

Yash wrote:
> In a perl 5.8 program, We have to use a long list of variables in a
> certain order in a number of places. It is something like:
> my ($var1, $field2, $a3, $b4, .....) ;
> .
> .
> .
> foreach $x ($var1, $field2, $a3, $b4, .....) {
> $x = -1 ;
> }
> .
> .
> .
> print join(",",($var1, $field2, $a3, $b4, .....)) ;
>
> Instead of having to repeat the names of the fields and their order,
> in all places of use, we would like to have something like
> #define FIELD_LIST $var1, $field2, $a3, $b4, .....


How about using a hash, and define the order in a constant:

use constant FIELD_LIST => qw(var1 field2 a3 b4);
my %hash = (var1 => 1, field2 => 2, a3 => 3, b4 => 4);

print join ',', @hash{ (FIELD_LIST) };

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
A. Sinan Unur

2004-09-29, 8:05 pm

Ron Parker <ron.parker@povray.org> wrote in
news:slrncllhr1.fdv.ron.parker@mail.parkrrrr.com:

> On 29 Sep 2004 13:07:09 GMT, A. Sinan Unur wrote:


>
> $ perldoc -f values
> =item values HASH
>
> Returns a list consisting of all the values of the named hash. (In a
> scalar context, returns the number of values.) The values are
> returned in an apparently random order.
> ----
>
> You don't think that "random order" thing might be a problem?


Of course I do ... But the OP's example set all variables to the same
value. My intent was for the OP to think about this a little and come up
with the appropriate question. There are several ways of dealing with this.
One quick & easy way would be to define the order:

my @order = qw(var1, field2, a3, b4);

and use a hash slice:

print join ',', @hash{@order};

There is also a FAQ entry that might be helpful.

--
A. Sinan Unur
1usa@llenroc.ude.invalid
(remove '.invalid' and reverse each component for email address)

A. Sinan Unur

2004-09-29, 8:05 pm

Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in news:2s045jF1evornU1@uni-
berlin.de:

> How about using a hash, and define the order in a constant:
>
> use constant FIELD_LIST => qw(var1 field2 a3 b4);
> my %hash = (var1 => 1, field2 => 2, a3 => 3, b4 => 4);
>
> print join ',', @hash{ (FIELD_LIST) };


Hi Gunnar,

Sorry, my response to Ron Parker's follow-up replicates your answer. I had
not seen your post yet.

--
A. Sinan Unur
1usa@llenroc.ude.invalid
(remove '.invalid' and reverse each component for email address)

Gunnar Hjalmarsson

2004-09-29, 8:05 pm

A. Sinan Unur wrote:
> Gunnar Hjalmarsson wrote:
>
> Sorry, my response to Ron Parker's follow-up replicates your
> answer. I had not seen your post yet.


No reason to apologize. It just confirms that it might be an
appropriate approach, right? ;-)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Shawn Corey

2004-09-29, 8:05 pm

Yash wrote:
> Hi,
>
> In a perl 5.8 program, We have to use a long list of variables in a
> certain order in a number of places. It is something like:
> my ($var1, $field2, $a3, $b4, .....) ;
> .
> .
> .
> foreach $x ($var1, $field2, $a3, $b4, .....) {
> $x = -1 ;
> }
> .
> .
> .
> print join(",",($var1, $field2, $a3, $b4, .....)) ;
>
>
> Instead of having to repeat the names of the fields and their order,
> in all places of use, we would like to have something like
> #define FIELD_LIST $var1, $field2, $a3, $b4, .....
>
> and user FIELD_LIST in all places.
> This is important for us as the fields and their order may change as
> the program evolves.
> Can somebody suggest a better alternative?
>
> Thanks


See perldoc perlrun for the -P option.

--- Shawn
Abigail

2004-09-30, 2:56 am

A. Sinan Unur (1usa@llenroc.ude.invalid) wrote on MMMMXLVII September
MCMXCIII in < URL:news:Xns95737A6C7670Aasu1cornelledu@
132.236.56.8>:
'' Ron Parker <ron.parker@povray.org> wrote in
'' news:slrncllhr1.fdv.ron.parker@mail.parkrrrr.com:
''
'' > On 29 Sep 2004 13:07:09 GMT, A. Sinan Unur wrote:
''
'' >>> print join ',', values %hash;
'' >
'' > $ perldoc -f values
'' > =item values HASH
'' >
'' > Returns a list consisting of all the values of the named hash. (In a
'' > scalar context, returns the number of values.) The values are
'' > returned in an apparently random order.
'' > ----
'' >
'' > You don't think that "random order" thing might be a problem?
''
'' Of course I do ... But the OP's example set all variables to the same
'' value. My intent was for the OP to think about this a little and come up
'' with the appropriate question. There are several ways of dealing with this.

The OP wrote:

> Instead of having to repeat the names of the fields and their order,


The last two words in that sentence look pretty clear to me.


While I think that problems defined by a single example make for bad
questions, I think that replies that only look at an example and
draw conclusions from that are equally worse.



Abigail
--
$_ = "\x3C\x3C\x45\x4F\x54"; s/<<EOT/<<EOT/e; print;
Just another Perl Hacker
EOT
Joe Smith

2004-09-30, 11:35 am

A. Sinan Unur wrote:

> my @order = qw(var1, field2, a3, b4);


use warnings;
my @order = qw(var1 field2 a3 b4);

-Joe
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com