#define-like feature in Perl

Y

Yash

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

A. Sinan Unur

(e-mail address removed) (Yash) wrote in
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
 
R

Ron Parker

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.
 
B

Brian Wakem

A. Sinan Unur said:
(e-mail address removed) (Yash) wrote in


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;
 
G

Gunnar Hjalmarsson

Yash said:
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) };
 
A

A. Sinan Unur

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.

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

A. Sinan Unur

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.
 
G

Gunnar Hjalmarsson

A. Sinan Unur said:
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? ;-)
 
S

Shawn Corey

Yash said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top