Using slices with 'my' to initialize hash keys and values.

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

  1. #1

    Default Using slices with 'my' to initialize hash keys and values.

    I have the following piece of code which works (meaning, does what I
    expect):

    #!/usr/bin/perl

    use strict;
    use warnings;

    my %translation;

    @translation {"A" .. "Z", 0 .. 9, ",", "?", "."} =
    qw/.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-..
    -- -. --- .--. --.- .-. ... - ..- ...- .-- -..-
    -.-- --.. ----- .---- ..--- ...-- ....- ..... -....
    --... ---.. ----. --..-- ..--.. .-.-.-/;

    foreach (sort keys %translation) {
    print "$_ = $translation{$_}\n";
    }



    The output is a list of alphanumerics, and punctuation, and its morse code
    representation.

    I'm not really interested in the output, but use it just to verify that I've
    loaded my hash as desired.


    Now I would like to tighten the code just a little. In particular, it seems
    un-Perlish to not be able to declare the hash and assign its key/value pairs
    all in one statement.

    But I cannot seem to find the way of declaring a hash while referring to it
    as an array for the purpose of assigning a list of keys and a list of values
    to it, all in one statement.

    It doesn't seem right to say:

    my @%translation .......

    And of course that just gets me a bunch of syntax errors.

    As does:
    my %(@translation) {......

    Is it possible to do what I'm considering, all in one nice slightly ugly
    statement?

    I've looked through the perldocs on hashes, slices, and a few other places,
    but I'm just not hitting on an example that turns on my lightbulb.

    Any comments and suggestions would be welcomed.

    Thanks

    Dave


    David Oswald Guest

  2. Similar Questions and Discussions

    1. DBI::mysql column names as hash keys?
      Hello, Given a DBI::mysql database with 2 tables and 5 columns like so: human --> name --> age --> sex dog --> breed --> colour
    2. multidimentional hash that has both two and three keys
      Can you have a multidimentional hash that has both two and three keys? For example: Array with two keys and a value: $HASH-> {$VAR}{letter}...
    3. hash of hash of array slices
      This works Foreach ( @{$hash{$key1}{$key2}} ) This does note Foreach ( @{($hash{$key1}{$key2})} ) This gives me this error .... Can't...
    4. Need help on Hash Slices. !!
      Hi, I have a list like @list = ( "key1: Vlan1 :0989\n" "key2: Vlan2 :0989\n" "key3: Vlan3 :0989\n" "key4: Vlan4 :0989\n"); I wanted to...
    5. Sort a hash based on values in the hash stored as arrays of hashes
      Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {...
  3. #2

    Default Re: Using slices with 'my' to initialize hash keys and values.

    David Oswald <spamblock@junkmail.com> wrote:
    > But I cannot seem to find the way of declaring a hash
    > while referring to it as an array for the purpose of
    > assigning a list of keys and a list of values to it, all
    > in one statement.
    I think this would be convenient:

    my @hash{@slice} = localtime;

    But you have to declare first and slice later:

    my %hash; @hash{@slice} = localtime;

    --
    Steve
    Steve Grazzini Guest

  4. #3

    Default Re: Using slices with 'my' to initialize hash keys and values.

    On Wed, 6 Aug 2003, David Oswald wrote:
    >my %translation;
    >
    >@translation {"A" .. "Z", 0 .. 9, ",", "?", "."} =
    > qw/.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-..
    > -- -. --- .--. --.- .-. ... - ..- ...- .-- -..-
    > -.-- --.. ----- .---- ..--- ...-- ....- ..... -....
    > --... ---.. ----. --..-- ..--.. .-.-.-/;
    >
    >Now I would like to tighten the code just a little. In particular, it seems
    >un-Perlish to not be able to declare the hash and assign its key/value pairs
    >all in one statement.
    Well, the problem is you can't declare a hash AND define a slice of it at
    the same time. The best I can do for you is:

    @$_{...keys...} = (...values...) for \my %hash;

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

    Jeff 'japhy' Pinyan 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