array to hash array

Hi,

Can someone tell me how to convert an array to hash.

Each array entry has a row of values
e.g. a(0) = ab cd ef; a(1) = mn de fg

The hash array needs to be constructed with one of the element in the
array row as the key.
e.g. hash{ab} = cd ef - ab is a string in the array row
hash{mn} = de fg - mn is a string in the array row

Can someone comment?

Regards,
Sharan

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Sharan Basappa [ Mo, 26 Juli 2010 12:22 ] [ ID #2045135 ]

AW: array to hash array

Sharan Basappa <sharan.basappa [at] gmail.com> asked:
> Can someone tell me how to convert an array to hash.
>
> Each array entry has a row of values
> e.g. a(0) =3D ab cd ef; a(1) =3D mn de fg
>
> The hash array needs to be constructed with one of the element in the
> array row as the key.
> e.g. hash{ab} =3D cd ef - ab is a string in the array row
> hash{mn} =3D de fg - mn is a string in the array row
>
> Can someone comment?

This works in the obvious way (pseudocode):

for each element in the source array:
compute the hash key from the element
compute the hash value that should be stored under that key
if the hash key doesn't exist yet
store the hash value
otherwise
handle duplicate hash key

Which part is it that is giving you trouble?

HTH,
Thomas

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
t.baetzler [ Mo, 26 Juli 2010 12:44 ] [ ID #2045136 ]

Re: array to hash array

Dear Sharan,

> Can someone tell me how to convert an array to hash.
>
> Each array entry has a row of values
> e.g. a(0) =3D ab cd ef; a(1) =3D mn de fg
>
> The hash array needs to be constructed with one of the element in the
> array row as the key.
> e.g. hash{ab} =3D cd ef =A0 =A0 =A0- ab is a string in the array row
> =A0 =A0 =A0 =A0hash{mn} =3D de fg =A0 =A0- mn is a string in the array ro=
w
>
> Can someone comment?

Here an example:

#!/usr/bin/perl

use strict;
use warnings;

# for debugging
use Data::Dumper;

my [at] array =3D (
'ab cd ef',
'mn de fg',
);

my %hash;

for my $element ( [at] array) {

if ($element =3D~ m{
^ # match the beginning of the
row
( \w{2} ) # match two characters
[ ] # match one space
(.*) # match the rest of the row
}mxs
) {
$hash{$1} =3D $2;
} else {
warn "error: incorrectly formatted array element: $element";
}

}

print Dumper \%hash;


Cheers,

Matteo


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Matteo Corti [ Mo, 26 Juli 2010 13:04 ] [ ID #2045206 ]

Re: array to hash array

On Jul 26, 3:22=A0am, sharan.basa... [at] gmail.com (Sharan Basappa) wrote:
> Hi,
>
> Can someone tell me how to convert an array to hash.
>
> Each array entry has a row of values
> e.g. a(0) =3D ab cd ef; a(1) =3D mn de fg
>
> The hash array needs to be constructed with one of the element in the
> array row as the key.
> e.g. hash{ab} =3D cd ef =A0 =A0 =A0- ab is a string in the array row
> =A0 =A0 =A0 =A0hash{mn} =3D de fg =A0 =A0- mn is a string in the array ro=
w
>

my %hash =3D map { split / ^ \S+ \K [ ]+ /x, $_ } [at] a;

See \K in perlre (5.10 or greater)
also perldoc -f split, perldoc -f map

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
derykus [ Di, 27 Juli 2010 17:39 ] [ ID #2045215 ]
Perl » gmane.comp.lang.perl.beginners » array to hash array

Vorheriges Thema: doubts in using Getopt in perl
Nächstes Thema: Data migration