Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

how to use two dimensional array?

by sankar R (Initiate)
on Jun 14, 2013 at 05:17 UTC ( [id://1038885]=perlquestion: print w/replies, xml ) Need Help??

sankar R has asked for the wisdom of the Perl Monks concerning the following question:

1. i want to access two elements in single index

2. for example i am having array1[10][2]

3. i want to use it as array1[0][0], array1[0][1], and array1[1][0],array1[1][1]

4. i increase first dimension but want to access two elements with fixed index is it possible?

5. give me some sample code for this concept

Replies are listed 'Best First'.
Re: how to use two dimensional array?
by muba (Priest) on Jun 14, 2013 at 05:57 UTC

    hi sankar R, welcome to Perlmonks! You might want to check the markup of your post, and throw in a few <code>...</code> tags to make it more readable.

    As to answer your question:

    1. If you want to access an element in a two-dimensional array, you'll need two indexes (one for each dimension). For an array to be two-dimensional, it just means that it is an array where each element is also an array (or more correctly, an array reference). If you just want to get the inner array without caring that it is an array, then you could do it with a single index.

    2. I infer that you mean that you have an array with 10 elements, each containing an array of 2 elements. Is this correct?

    3. Ok, that's clear. You want to access the elements of the inner arrays one by one.

    4. Well...
    a) What have you tried?
    b) How did it work?
    c) How didn't it work?
    d) What output did you expect?
    e) What output did you get, including error messages if any?

    How does this fail you?

    use strict; use warnings; my @array1 = ( ["first #1", "first #2"], ["second #1", "second #2"], ["third #1", "third #2"], ); my $outer_idx = 0; my $inner_idx = 0; printf "One: %s; Two: %s\n", $array1[$outer_idx]->[0], $array1[$outer_ +idx]->[1]; $outer_idx++; printf "One: %s; Two: %s\n", $array1[$outer_idx]->[0], $array1[$outer_ +idx]->[1];

    Output:

    One: first #1; Two: first #2 One: second #1; Two: second #2

    5. I think I did that in point 4. :) Cheers.

Re: how to use two dimensional array?
by 2teez (Vicar) on Jun 14, 2013 at 05:35 UTC

    Hi sankarR,

    Welcome to the Monastery.

    ..give me some sample code for this concept..

    Before you get started, I think you should read How do I post a question effectively?.
    Apply what you have read, then you would get all the help you ever needed.

    You can also check the following Documentation:

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: how to use two dimensional array?
by BillKSmith (Monsignor) on Jun 14, 2013 at 10:47 UTC
    You may find this syntax for convenient.
    use strict; use warnings; my @array = ( [1,2], [7,5], [4,4], [2,2], [0,7], [3,4], [8,9], [3,6], [5,7], [7,2], ); foreach my $pair (@array) { my ($x, $y) = @$pair; print "$x $y \n"; }
    Bill

      One of my favorite tidbits of the language - instead of your for loop:

      print "@$_\n" for @array;
        Very clever way of getting the same output, but I do not think it answers the original question. Also note your dependence om the default value of the special variable $LIST_SEPARATOR ($").
        Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1038885]
Approved by tobyink
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found