Need help on Hash Slices. !!

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

  1. #1

    Default 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 make a hash with keys as key1, key2 , key3, key4.

    I want to write something like this :
    **********************************************
    my @keys, @values, @key_to_value;
    (@keys, @values) = map /(key\d+): (\w+).*?$/, @list;
    @key_to_value{@keys} = @values;

    But the problem is the map function just creates one list i.e. @keys .. so I am not able to make the hash.

    ie .. I want to extract @keys and @values in one go and then use the HASH SLICE to get the desired result.

    Can some one help me. The only this is I don't want to extract @keys and @values separately.

    Regards
    Rajeev
    Pandey Rajeev-A19514 Guest

  2. Similar Questions and Discussions

    1. 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...
    2. 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. Help with slices
      Hi, I have a few images some are gifs some are jpegs which I have optimized and now I want to put them on my website. Problem: Whenever I try to...
    4. Another reference question (hash of hash references)
      beginners, I am trying to build a hash of hash references. My problem is that I need to be able to add a key/value pair to the internal hashes......
    5. 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;
  3. #2

    Default RE: Need help on Hash Slices. !!

    Pandey Rajeev-A19514 wrote:
    > 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 make a hash with keys as key1, key2 , key3, key4.
    >
    > I want to write something like this :
    > **********************************************
    > my @keys, @values, @key_to_value;
    > (@keys, @values) = map /(key\d+): (\w+).*?$/, @list;
    > @key_to_value{@keys} = @values;
    >
    > But the problem is the map function just creates one list
    > i.e. @keys .. so I am not able to make the hash.
    >
    > ie .. I want to extract @keys and @values in one go and then
    > use the HASH SLICE to get the desired result.
    >
    > Can some one help me. The only this is I don't want to
    > extract @keys and @values separately.
    Since map is returning a list in the form key, value, key, value, ...
    you can just assign directly to a hash:

    my %key_to_value = map /(key\d+): (\w+)/, @list;

    Bob Showalter Guest

  4. #3

    Default RE: Need help on Hash Slices. !!

    > Hi,

    Howdy
    >
    > 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 make a hash with keys as key1, key2 , key3, key4.
    >
    Soemthing like this perhaps?

    ++UNTESTED++

    my %hsh;
    for(@list) {
    my($k,$v) = $_ =~ m/(^\w+\):(.*)/;
    $hsh{$k} = $v;
    }

    HTH

    DMuey
    > I want to write something like this :
    > **********************************************
    > my @keys, @values, @key_to_value;
    > (@keys, @values) = map /(key\d+): (\w+).*?$/, @list;
    > @key_to_value{@keys} = @values;
    >
    > But the problem is the map function just creates one list
    > i.e. @keys .. so I am not able to make the hash.
    >
    > ie .. I want to extract @keys and @values in one go and then
    > use the HASH SLICE to get the desired result.
    >
    > Can some one help me. The only this is I don't want to
    > extract @keys and @values separately.
    >
    > Regards
    > Rajeev
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    >
    >
    Dan Muey Guest

  5. #4

    Default RE: Need help on Hash Slices. !!

    Hi Bob,

    There is a simple problem here
    ********************
    The file is -->

    TEST_CASE_FILE_PATH = ../testcases/CAT/
    TEST_CASE_OUTPUT_PATH = ../testlogs/
    SERVER_NAME = sunomcr
    ********************
    $VAR1 = 'TEST_CASE_FILE_PATH';
    $VAR2 = '../testcases/CAT/';
    $VAR3 = 'SERVER_NAME';
    $VAR4 = 'sunomcr';
    $VAR5 = 'TEST_CASE_OUTPUT_PATH';
    $VAR6 = '../testlogs/';

    foreach $key (%hash_line) { print "$key => $hash_line{$key}\n";}
    The output is like this -->

    TEST_CASE_FILE_PATH => ../testcases/CAT/
    .../testcases/CAT/ =>
    SERVER_NAME => sunomcr
    sunomcr =>
    TEST_CASE_OUTPUT_PATH => ../testlogs/
    .../testlogs/ =>

    It seems it takes all the elements in the list as the key.

    Regards
    Rajeev

    -----Original Message-----
    From: Bob Showalter [mailto:Bob_Showalter@taylorwhite.com]
    Sent: Tuesday, September 23, 2003 8:56 PM
    To: 'Pandey Rajeev-A19514'; [email]beginners@perl.org[/email]
    Subject: RE: Need help on Hash Slices. !!


    Pandey Rajeev-A19514 wrote:
    > 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 make a hash with keys as key1, key2 , key3, key4.
    >
    > I want to write something like this :
    > **********************************************
    > my @keys, @values, @key_to_value;
    > (@keys, @values) = map /(key\d+): (\w+).*?$/, @list;
    > @key_to_value{@keys} = @values;
    >
    > But the problem is the map function just creates one list
    > i.e. @keys .. so I am not able to make the hash.
    >
    > ie .. I want to extract @keys and @values in one go and then
    > use the HASH SLICE to get the desired result.
    >
    > Can some one help me. The only this is I don't want to
    > extract @keys and @values separately.
    Since map is returning a list in the form key, value, key, value, ...
    you can just assign directly to a hash:

    my %key_to_value = map /(key\d+): (\w+)/, @list;
    Pandey Rajeev-A19514 Guest

  6. #5

    Default RE: Need help on Hash Slices. !!

    Pandey Rajeev-A19514 wrote:
    > Hi Bob,
    >
    > There is a simple problem here
    > ********************
    > The file is -->
    >
    > TEST_CASE_FILE_PATH = ../testcases/CAT/
    > TEST_CASE_OUTPUT_PATH = ../testlogs/
    > SERVER_NAME = sunomcr
    > ********************
    > $VAR1 = 'TEST_CASE_FILE_PATH';
    > $VAR2 = '../testcases/CAT/';
    > $VAR3 = 'SERVER_NAME';
    > $VAR4 = 'sunomcr';
    > $VAR5 = 'TEST_CASE_OUTPUT_PATH';
    > $VAR6 = '../testlogs/';
    >
    > foreach $key (%hash_line) { print "$key => $hash_line{$key}\n";}
    > The output is like this -->
    >
    > TEST_CASE_FILE_PATH => ../testcases/CAT/
    > ../testcases/CAT/ =>
    > SERVER_NAME => sunomcr
    > sunomcr =>
    > TEST_CASE_OUTPUT_PATH => ../testlogs/
    > ../testlogs/ =>
    >
    > It seems it takes all the elements in the list as the key.
    >
    > Regards
    > Rajeev
    I'm sorry, I don't see what any of this has to do with what you originally
    posted. Perhaps we need to start over.
    >
    > -----Original Message-----
    > From: Bob Showalter [mailto:Bob_Showalter@taylorwhite.com]
    > Sent: Tuesday, September 23, 2003 8:56 PM
    > To: 'Pandey Rajeev-A19514'; [email]beginners@perl.org[/email]
    > Subject: RE: Need help on Hash Slices. !!
    >
    >
    > Pandey Rajeev-A19514 wrote:
    > > 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 make a hash with keys as key1, key2 , key3, key4.
    > >
    > > I want to write something like this :
    > > **********************************************
    > > my @keys, @values, @key_to_value;
    > > (@keys, @values) = map /(key\d+): (\w+).*?$/, @list;
    > > @key_to_value{@keys} = @values;
    > >
    > > But the problem is the map function just creates one list
    > > i.e. @keys .. so I am not able to make the hash.
    > >
    > > ie .. I want to extract @keys and @values in one go and then
    > > use the HASH SLICE to get the desired result.
    > >
    > > Can some one help me. The only this is I don't want to
    > > extract @keys and @values separately.
    >
    > Since map is returning a list in the form key, value, key, value, ...
    > you can just assign directly to a hash:
    >
    > my %key_to_value = map /(key\d+): (\w+)/, @list;
    Bob Showalter Guest

  7. #6

    Default Re: Need help on Hash Slices. !!

    On Tue, Sep 23, 2003 at 08:49:11PM +0530, Pandey Rajeev-A19514 wrote:
    > 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 make a hash with keys as key1, key2 , key3, key4.
    >
    > I want to write something like this :
    > **********************************************
    > my @keys, @values, @key_to_value;
    > (@keys, @values) = map /(key\d+): (\w+).*?$/, @list;
    > @key_to_value{@keys} = @values;
    >
    > But the problem is the map function just creates one list i.e. @keys .. so I am not able to make the hash.
    >
    > ie .. I want to extract @keys and @values in one go and then use the HASH SLICE to get the desired result.
    >
    > Can some one help me. The only this is I don't want to extract @keys and @values separately.
    >
    > Regards
    > Rajeev

    Hi Rajeev,

    Sounds like you want to create the hash, the @keys list, and the
    @values list in one pass, right? Ok, this should do it:

    my (@keys, @values, %hash);
    my @list = ( "key1: Vlan1 :0989\n",
    "key2: Vlan2 :0989\n",
    "key3: Vlan3 :0989\n",
    "key4: Vlan4 :0989\n",
    );
    do { /(key\d+): (\w+).*?$/; push @keys, $1; push @values, $2; $hash{$1}=$2; } for @list;



    You can't use map here, because map always returns a single list, and
    you are trying to build up multiple lists at once. (Or, rather, you
    COULD use it, instead of the for, but it would be pointless as you
    would simply be throwing out the list that it returns.)

    (Oh, and you needed commas after your list elements.)


    --Dks
    David Storrs 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