Ask a Question related to PERL Beginners, Design and Development.
-
Pandey Rajeev-A19514 #1
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
-
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... -
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 = {... -
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... -
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...... -
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; -
Bob Showalter #2
RE: Need help on Hash Slices. !!
Pandey Rajeev-A19514 wrote:
Since map is returning a list in the form key, value, key, value, ...> 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.
you can just assign directly to a hash:
my %key_to_value = map /(key\d+): (\w+)/, @list;
Bob Showalter Guest
-
Dan Muey #3
RE: Need help on Hash Slices. !!
> Hi,
Howdy
Soemthing like this perhaps?>
> 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.
>
++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
-
Pandey Rajeev-A19514 #4
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:Since map is returning a list in the form key, value, key, value, ...> 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.
you can just assign directly to a hash:
my %key_to_value = map /(key\d+): (\w+)/, @list;
Pandey Rajeev-A19514 Guest
-
Bob Showalter #5
RE: Need help on Hash Slices. !!
Pandey Rajeev-A19514 wrote:
I'm sorry, I don't see what any of this has to do with what you originally> 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
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
-
David Storrs #6
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



Reply With Quote

