better way of building string from hash

Ask a Question related to PERL Miscellaneous, Design and Development.

  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. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. printf? help building hash on-the-fly?
      > -----Original Message----- To pad with zeroes, use %03d. %3d pads with spaces. -Mark
    4. 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):...
    5. 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...
  3. #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

  4. #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

  5. #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-documented AS you write it.


    Sundial Services Guest

  6. #5

    Default Re: better way of building string from hash

    >>>>> "SS" == Sundial Services <info_ns5@sundialservices.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-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

Posting Permissions

  • You may not post new threads
  • You may 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