better way of building string from hash

M

mike solomon

I have written the following code to create a variable in the format of
'code' , 'value' , 'description'

I know i could have just said:

my $arrField = "'code' , 'value' , 'description'";

but i think it is neater to put the code pairs into a hash

What I want to know if if there is a better way of doing this, as the
way I have come up with is a bit messy



my %required = (
code => "Code is required",
value => "Value is required",
description => "Description is required",
);

my @arrField ;
my @arrMess;

for my $key (keys %required) {
push @arrMess , qq {'$required{$key}'};
push @arrField, qq {'$key'};
}

my $arrMess = join "," , @arrMess;
my $arrField = join "," , @arrField;



Regards

Mike Solomon
 
S

Steven Kuo

On Thu, 17 Jul 2003, mike solomon wrote:

(snipped)
What I want to know if if there is a better way of doing this, as the
way I have come up with is a bit messy



my %required = (
code => "Code is required",
value => "Value is required",
description => "Description is required",
);

my @arrField ;
my @arrMess;

for my $key (keys %required) {
push @arrMess , qq {'$required{$key}'};
push @arrField, qq {'$key'};
}

my $arrMess = join "," , @arrMess;
my $arrField = join "," , @arrField;



Regards

Mike Solomon



This is more legible to me:

my $string_from_keys = join ', ' => map qq{'$_'} => keys %required;
my $string_from_values = join ', ' => map qq{'$_'} => values %required;
 
M

mike solomon

Steven said:
On Thu, 17 Jul 2003, mike solomon wrote:

(snipped)





This is more legible to me:

my $string_from_keys = join ', ' => map qq{'$_'} => keys %required;
my $string_from_values = join ', ' => map qq{'$_'} => values %required;

Steven

Thats great, much neater

Thanks

I have a blind spot about using map
I must learn more about it
 
S

Sundial Services

John said:
You could do it like this:

my $arrMess = join ',', map "'$_'", values %required;
my $arrField = join ',', map "'$_'", keys %required;

Just remember though ... "it has to be clear." Abundantly clear. And it
has to be resistant to side-effects, caused by an unrelated change made to
the software at some future time. (A change, that is, that you intended to
be "unrelated," but because of the original design, "suh-prize!")

Perl teeters on being a "write-only language." Keep your code simple and
direct, and extremely well-documented AS you write it.
 
U

Uri Guttman

SS> Just remember though ... "it has to be clear." Abundantly clear.
SS> And it has to be resistant to side-effects, caused by an unrelated
SS> change made to the software at some future time. (A change, that
SS> is, that you intended to be "unrelated," but because of the
SS> original design, "suh-prize!")

and you think join and map will change and are unclear?

SS> Perl teeters on being a "write-only language." Keep your code
SS> simple and direct, and extremely well-documented AS you write it.

all langs can be write only. it is the coder's issue and not the
language. this is patent FUD and you should stop spreading it.

uri
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top