Ask a Question related to PERL Beginners, Design and Development.
-
Kevin Old #1
Build multidimensional array from arrays
Hello everyone,
I have:
@one = qw(A B C D);
@two = qw(E F G H);
I want to build a multidimensional array from the above arrays. I want
to put @one in the first "column" of the array. I want to put @two in
the second "column" of the array.
I want the resulting MDA to look like:
@mda = (
[A][E],
[B][F],
[C][G],
[D][H]
);
Any ideas? Sorry if this is not clear.
Kevin
--
Kevin Old <kold@kold.homelinux.com>
Kevin Old Guest
-
Multidimensional arrays
Hi Can anybody help me grasp multidimensional arrays please? Basically, I am retrieving all records from one table (two fields), and want to... -
sorting multidimensional arrays
One last question about multi-dimensional arrays - I'm aware of the function array_multisort, but that keeps the relationship with the key - what do... -
how to make multidimensional arrays?
Guys, I'd like to enter the data below into a multi-dimensional array - this is my first time attempting this, so I'm not sure of the syntax in how... -
Split multidimensional array into 4 multidimensional arrays
Hello everyone, I have a multidimensional array that I need to split into 4 multidimensional arrays. I've tried the examples from the... -
[PHP] Multidimensional arrays
<snip> select id, name from customer redim custarray(recordcount,2) i = 0 while not eof custarray(i,0) = id; custarray(i,1) = name; i = i+1;... -
Jeff 'Japhy' Pinyan #2
Re: Build multidimensional array from arrays
On Jan 29, Kevin Old said:
Do you mean [A, E]?>@one = qw(A B C D);
>@two = qw(E F G H);
>
>
>@mda = (
> [A][E],
If so, this is how I'd do it:> [B][F],
> [C][G],
> [D][H]
>);
@mda = map [ $one[$_], $two[$_] ], 0 .. $#one;
If you need an explanation, feel free to ask.
--
Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
Jeff 'Japhy' Pinyan Guest
-
Kevin Old #3
Re: Build multidimensional array from arrays
On Thu, 2004-01-29 at 12:34, Jeff 'japhy' Pinyan wrote:
Well, no that wasn't what I was looking for, but it's a nice piece of> On Jan 29, Kevin Old said:
>>> >@one = qw(A B C D);
> >@two = qw(E F G H);
> >
> >
> >@mda = (
> > [A][E],
> Do you mean [A, E]?
>>> > [B][F],
> > [C][G],
> > [D][H]
> >);
> If so, this is how I'd do it:
>
> @mda = map [ $one[$_], $two[$_] ], 0 .. $#one;
>
> If you need an explanation, feel free to ask.
code to add to my arsenal. That basically puts the contents of @one in
the first *row* of the @mda, @two in the second *row*. I want it to go
in @mda in the *columns*. So...
$mda[0][0] = A;
$mda[0][1] = B;
so forth...
$mda[1][0] = E;
$mda[1][1] = F;
so forth...
A little background on why I want to do this is that I'm formatting data
in arrays to that I can send the Spreadsheet::WriteExcel module a
reference to an MDA so that it writes the spreadsheet "contents" from
one call to
$ws->write(0, 0, \@mda); #writes all data into spreadsheet
Thanks,
Kevin
--
Kevin Old <kold@kold.homelinux.com>
Kevin Old Guest
-
Jeff 'Japhy' Pinyan #4
Re: Build multidimensional array from arrays
On Jan 29, Kevin Old said:
Oh, ok.>On Thu, 2004-01-29 at 12:34, Jeff 'japhy' Pinyan wrote:>>> On Jan 29, Kevin Old said:
>>>> >@one = qw(A B C D);
>> >@two = qw(E F G H);
>Well, no that wasn't what I was looking for, but it's a nice piece of
>code to add to my arsenal. That basically puts the contents of @one in
>the first *row* of the @mda, @two in the second *row*. I want it to go
>in @mda in the *columns*. So...
>
>$mda[0][0] = A;
>$mda[0][1] = B;
>
>$mda[1][0] = E;
>$mda[1][1] = F;
@mda = ([@one], [@two]);
# or
@mda = (\@one, \@two);
--
Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
Jeff 'Japhy' Pinyan Guest
-
Rob Dixon #5
Re: Build multidimensional array from arrays
Kevin Old wrote:
Too many ideas! It would have helped if @mda was built in valid Perl>
> I have:
>
> @one = qw(A B C D);
> @two = qw(E F G H);
>
> I want to build a multidimensional array from the above arrays. I want
> to put @one in the first "column" of the array. I want to put @two in
> the second "column" of the array.
>
> I want the resulting MDA to look like:
>
> @mda = (
> [A][E],
> [B][F],
> [C][G],
> [D][H]
> );
>
> Any ideas? Sorry if this is not clear.
syntax and referred to the arrays that you say it must be built from.
Here's a starting point:
my @one = qw(A B C D);
my @two = qw(E F G H);
my @mda;
my $i = 0;
foreach (@one) {
push @mda, [$one[$i], $two[$i]];
$i++;
}
but /please/ go back a few steps and explain what you want you're
trying to do. I doubt that putting characters 'A' .. 'H' into
an array is your goal. What you're starting with and what end you
have in mind is fundamental. Abstract as little as you can.
Rob
Rob Dixon Guest
-
Kevin Old #6
Re: Build multidimensional array from arrays
On Thu, 2004-01-29 at 14:17, Rob Dixon wrote:
Rob,> but /please/ go back a few steps and explain what you want you're
> trying to do. I doubt that putting characters 'A' .. 'H' into
> an array is your goal. What you're starting with and what end you
> have in mind is fundamental. Abstract as little as you can.
>
> Rob
Ok, I finally got my script to do what I want with the code below.
my @tmp = ($custno, $cname, $arcode, $invbal, $curr, $thirty, $sixty,
$ninety, $ovrninety, $ile, $ca);
for (my $j = 0; $j < $#tmp; $j++) {
$data[$j][$i] = $tmp[$j];
}
$i++;
What I'm doing is building an MDA of data to send to the
Spreadsheet::WriteExcel module. Think of the MDA as a Spreadsheet. I'm
trying to format the data (built by growing it one row/column at a time)
inside the MDA as I'd like to see it in the Spreadsheet.
I do this a lot and am trying to come up with a few subroutines to
"display" my data correctly in a MDA so that I can send the data to the
S:WE module in one call
$ws->write(0, 0, \@mda); and go about my business. The code above
"displays" my data in "rows".
If I just said:
push @data, [@tmp];
My data would be displayed in a "column".
I'd like to be able to send any array of values, for example @tmp from
above, to a subroutine, for example &displayVert, and it would append
that to a MDA as a "column". If I sent it to &displayHoriz, it would be
appended as a "row".
I know this is kind of lazy to format my data before sending it to
S::WE, but who can blame me....I'm a Perl programmer....it's in my
blood.
Thoughts, suggestions, insults?
Thanks for all your help,
Kevin
--
Kevin Old <kold@kold.homelinux.com>
Kevin Old Guest



Reply With Quote

