Ask a Question related to PERL Miscellaneous, Design and Development.
-
mike solomon #1
better way of building string from hash
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
mike solomon Guest
-
help building hash on-the-fly?
Hello... This is probably a very simple question, but I don't have much experience using hashes... I have a simple program that lists all of the... -
printf? help building hash on-the-fly?
McMahon, Chris wrote: I like things to look right and if you have more than 9 items, then you don't line up like I would like it. I very seldom use... -
printf? help building hash on-the-fly?
> -----Original Message----- To pad with zeroes, use %03d. %3d pads with spaces. -Mark -
printf? help building hash on-the-fly?
Hi Wags... So now an idle style question, if you don't mind... This syntax seems pretty obscure to me (I had to look up what printf was doing):... -
Building Hash of Arrays from multiple files
"Garry Short" <g4rry_short@zw4llet.com> wrote in message news:bf68hs$qjs$1$8300dec7@news.demon.co.uk... you must "push" onto an array, so you... -
Steven Kuo #2
Re: better way of building string from hash
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;
--
Hope this helps,
Steven
Steven Kuo Guest
-
mike solomon #3
Re: better way of building string from hash
Steven Kuo wrote:Steven> 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;
>
Thats great, much neater
Thanks
I have a blind spot about using map
I must learn more about it
mike solomon Guest
-
Sundial Services #4
Re: better way of building string from hash
John W. Krahn wrote:
Just remember though ... "it has to be clear." Abundantly clear. And it> mike solomon wrote:>>>
>> 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;
> You could do it like this:
>
> my $arrMess = join ',', map "'$_'", values %required;
> my $arrField = join ',', map "'$_'", keys %required;
>
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.
Sundial Services Guest
-
Uri Guttman #5
Re: better way of building string from hash
>>>>> "SS" == Sundial Services <info_ns5@sundialservices.com> writes:
SS> Just remember though ... "it has to be clear." Abundantly clear.>> my $arrMess = join ',', map "'$_'", values %required;
>> my $arrField = join ',', map "'$_'", keys %required;
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
--
Uri Guttman ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
Uri Guttman Guest



Reply With Quote

