Professional Web Applications Themes

better way of building string from hash - PERL Miscellaneous

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

  1. #1

    Default 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

  2. #2

    Default 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

  3. #3

    Default Re: better way of building string from hash



    Steven Kuo wrote:
    > 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;
    >
    Steven

    Thats great, much neater

    Thanks

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




    mike solomon Guest

  4. #4

    Default Re: better way of building string from hash

    John W. Krahn wrote:
    > 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;
    >
    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-doented AS you write it.


    Sundial Services Guest

  5. #5

    Default Re: better way of building string from hash

    >>>>> "SS" == Sundial Services <info_ns5sundialservices.com> writes:
    >> my $arrMess = join ',', map "'$_'", values %required;
    >> my $arrField = join ',', map "'$_'", keys %required;
    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-doented 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]uristemsystems.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

Similar Threads

  1. help building hash on-the-fly?
    By Chris McMahon in forum PERL Beginners
    Replies: 4
    Last Post: November 6th, 12:26 AM
  2. printf? help building hash on-the-fly?
    By David --- Senior Programmer Analyst --- Wgo Wagner in forum PERL Beginners
    Replies: 0
    Last Post: November 5th, 11:49 PM
  3. printf? help building hash on-the-fly?
    By Mark Lobue in forum PERL Beginners
    Replies: 1
    Last Post: November 5th, 11:33 PM
  4. printf? help building hash on-the-fly?
    By Chris McMahon in forum PERL Beginners
    Replies: 1
    Last Post: November 5th, 11:21 PM
  5. Building Hash of Arrays from multiple files
    By joeri in forum PERL Miscellaneous
    Replies: 3
    Last Post: July 17th, 03:31 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139