For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2005 > Example of hash and array movement correct?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Example of hash and array movement correct?
Bastian Angerstein

2005-02-18, 3:56 pm


Hello,
I am looking for ways to move data
between array and hashes.

Does anybody has a better way or idea to do some of this:

Example 1:
@indatas = $datas [0 .. 4];
Example 2:
$nkey = $dataln[3] . $dataln[4];
Example 3:
$base{$bv[0]} = [ $bv[1], $bv[2], $bv[3], $bv[4] ];
Errin M HMMA/IT Larsen

2005-02-18, 3:56 pm


> Subject: Example of hash and array movement correct?
>=20
>=20
>=20
> Hello,=20



Hello


> I am looking for ways to move data=20
> between array and hashes.
>=20


In all 3 of your examples, you don't show us the declaration of your
variables nor how they are filled with data. I can make some
assumptions, though.


> Does anybody has a better way or idea to do some of this:
> =20
> Example 1:
> @indatas =3D $datas [0 .. 4];



If I assume that the @datas variable is declared like this:
my @datas =3D qw( One Two Three Four Five Six Seven );

then I can take a slice of that array to initiate the next array:
my @indatas =3D @datas[0 .. 4];

I think in Example 1, you just need to reference the @datas array with
an '@' sign at the front (instead of a '$' sign) when you are using it
to create a slice.


> Example 2:
> $nkey =3D $dataln[3] . $dataln[4];



If I assume that the @dataln variable is declared like this:
my @dataln =3D qw( A B C D E F G H );

then I can easily create a scalar variable, $nkey, by concatanating
two of the elements of the @dataln array:
my $nkey =3D $dataln[3] . $dataln[4];

It looks to me that you got this one right. Is this what you were
asking?


> Example 3:
> $base{$bv[0]} =3D [ $bv[1], $bv[2], $bv[3], $bv[4] ];



If I assume that the @bv array and the %base hash are declared like
this:
my @bv =3D qw( Alpha Bravo Charlie Delta Echo );
my %base;

then I can initiate an element of that hash with a slice from the @bv
array:
$base{$bv[0]} =3D [@bv[1 .. 4]];

Again, when taking a slice from an array, you need to reference the
array with the '@' sign. Your example works, but you asked for
simpler/easier ways to do things.



Were these examples helpful? Maybe if you gave us a better idea of
what you wanted to do with what kind of data we could be more specific
helping you.

--Errin
Charles K. Clarkson

2005-02-18, 3:56 pm

Bastian Angerstein <ang@nmc-m.dtag.de> wrote:

: Hello,
: I am looking for ways to move data
: between array and hashes.
:
: Does anybody has a better way or idea to do some of this:

Not really. What you have works, though the first example
will throw a warning under strictures and warnings.


: Example 1:
: @indatas = $datas [0 .. 4];

Better written as:

@indatas = @datas[0 .. 4];


: Example 2:
: $nkey = $dataln[3] . $dataln[4];

Looks okay.


: Example 3:
: $base{$bv[0]} = [ $bv[1], $bv[2], $bv[3], $bv[4] ];

Less typing:

$base{$bv[0]} = [ @bv[1 .. 4] ];

HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com