For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > How to create arrays of arrays?









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 How to create arrays of arrays?
Siegfried Heintze

2005-04-19, 8:55 pm

Hey Perlers!

This code used to work and then I changed something and it no longer works!
The problem is the output no longer includes "string 2a" and "string 2b".
What am I doing wrong?

How do explicitly assign an array (not an array reference, but an array) to
an array cell?

Thanks,
Sieg


sub concat {
# All arguments are assumed to be strings or nested arrays of strings.
# Concatenate all elements of all arguments.
return join "", map ref eq "ARRAY" ? concat( @$_ ) : $_, @_ }

my @sResult = ();
push @sResult, "string 1";
push @sResult, "string 2";

my $nMark = @sResult;

push @sResult, (); # make an array of an array cell: does not work

push @sResult = "string 3";

$sResult[$nMark][@{$sResult[$nMark]}] = "string 2a";
$sResult[$nMark][@{$sResult[$nMark]}] = "string 2b";

print concat \@sResult;

Wagner, David --- Senior Programmer Analyst --- WG

2005-04-19, 8:55 pm

Siegfried Heintze wrote:
> Hey Perlers!
>=20
> This code used to work and then I changed something and it no longer
> works! The problem is the output no longer includes "string 2a" and
> "string 2b". What am I doing wrong?
>=20
> How do explicitly assign an array (not an array reference, but an
> array) to an array cell?
>=20
> Thanks,
> Sieg
>=20
>=20
> sub concat {
> # All arguments are assumed to be strings or nested arrays of
> strings. # Concatenate all elements of all arguments.
> return join "", map ref eq "ARRAY" ? concat( @$_ ) : $_, @_ }
>=20
> my @sResult =3D ();
> push @sResult, "string 1";
> push @sResult, "string 2";
>=20
> my $nMark =3D @sResult;
>=20
> push @sResult, (); # make an array of an array cell: does not work
>=20
> push @sResult =3D "string 3";

=09
>=20
> $sResult[$nMark][@{$sResult[$nMark]}] =3D "string 2a";
> $sResult[$nMark][@{$sResult[$nMark]}] =3D "string 2b";
>=20

If you want the array, then change above two lines:

$sResult[$nMark] =3D ["string 2a", "string 2b"] ;
=09
But the way this is, the 'string 3' will be replaced. if want to keep the =
string 3, then I did a=20

if ( defined $sResult[$nMark] ) {
$sResult[$nMark] =3D [$sResult[$nMark], "string 2a", "string 2b"] ;

}else {
$sResult[$nMark] =3D ["string 2a", "string 2b"];
}

The output with this last one is:
string 1string 2string 3string 2astring 2b

But unsure is that what you are after.

Wags ;)
}> print concat \@sResult;



****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************
***************

Siegfried Heintze

2005-04-19, 8:55 pm

See comments in line:

>
> If you want the array, then change above two lines:
>
> $sResult[$nMark] = ["string 2a", "string 2b"] ;


(1) Does $sResult[$nMark] contain an array reference or an array? I want
it to contain an array.


>
> But the way this is, the 'string 3' will be replaced. if want to

keep the string 3, then I did a
>


(2) Why is this necessary? I did a push to create a gap. Why is not my
"push @sResult, ();" creating a gap?

> if ( defined $sResult[$nMark] ) {
> $sResult[$nMark] = [$sResult[$nMark], "string 2a", "string 2b"] ;
>
> }else {
> $sResult[$nMark] = ["string 2a", "string 2b"];
> }
>



Thanks!

Siegfried

Wagner, David --- Senior Programmer Analyst --- WG

2005-04-19, 8:55 pm

Siegfried Heintze wrote:
> See comments in line:
>=20
>=20
> (1) Does $sResult[$nMark] contain an array reference or an array? I
> want it to contain an array.
>=20

When you look at the output fo Data::Dumper:
$VAR1 =3D [
'string 1',
'string 2',
[
'string 3',
'string 2a',
'string 2b'
]
];
It is an array.

Wags ;)
>=20
> keep the string 3, then I did a
>=20
> (2) Why is this necessary? I did a push to create a gap. Why is not
> my "push @sResult, ();" creating a gap?
>=20
>=20
>=20
> Thanks!
>=20
> Siegfried




****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************
***************

John Doe

2005-04-20, 3:56 pm

Hi Siegfried

Am Dienstag, 19. April 2005 23.21 schrieb Siegfried Heintze:
> Hey Perlers!
>
> This code used to work and then I changed something


_What_ did you change?
If you don't k know: Use some version control system, so you can reconstruct
all changes.

> and it no longer works!
> The problem is the output no longer includes "string 2a" and "string 2b".
> What am I doing wrong?


put

use strict;
use warnigs;

ot top of every script. I get the following with your code below:

Useless use of push with no values at - line 15.

> How do explicitly assign an array (not an array reference, but an array) to
> an array cell?


You can't... an array element can only contain a scalar.

> Thanks,
> Sieg
>
>
> sub concat {
> # All arguments are assumed to be strings or nested arrays of strings.
> # Concatenate all elements of all arguments.
> return join "", map ref eq "ARRAY" ? concat( @$_ ) : $_, @_ }
>
> my @sResult = ();
> push @sResult, "string 1";
> push @sResult, "string 2";
>
> my $nMark = @sResult;
>
> push @sResult, (); # make an array of an array cell: does not work


perl -le '
use strict; use warnings;
my @a =(1,2,3);
push @a, ();
print join "-", @a;
'
# prints:
1-2-3

> push @sResult = "string 3";


push @sResult, "string 3";

> $sResult[$nMark][@{$sResult[$nMark]}] = "string 2a";
> $sResult[$nMark][@{$sResult[$nMark]}] = "string 2b";
>
> print concat \@sResult;


hth, joe

Sponsored Links







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

Copyright 2009 codecomments.com