Build multidimensional array from arrays

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. [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;...
  3. #2

    Default Re: Build multidimensional array from arrays

    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.

    --
    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

  4. #3

    Default Re: Build multidimensional array from arrays

    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);
    > >
    > >
    > >@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.
    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;
    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

  5. #4

    Default Re: Build multidimensional array from arrays

    On Jan 29, Kevin Old said:
    >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;
    Oh, ok.

    @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

  6. #5

    Default Re: Build multidimensional array from arrays

    Kevin Old wrote:
    >
    > 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.
    Too many ideas! It would have helped if @mda was built in valid Perl
    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

  7. #6

    Default Re: Build multidimensional array from arrays

    On Thu, 2004-01-29 at 14:17, Rob Dixon wrote:
    > 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,

    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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139