Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2D array 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Is there a 2D array in prel?
(I mean like in c++ when you do int a[10][10];)
 
sure..... in Perl (or Prel which, I think is a shampoo).... <smile>, the are two basic array types, simple arrays and associative arrays....

simple arrays are named with an '@' sign.
# create a list of names
@names = ('joe','jane','david','william');

# get the second element from the list ( count from zero)
# note that we don't use @ when refering to one element in the list.
# the [] tell perl that you are asking for an element from a list.
$second_name = $names[1];

# see perl functions pop, shift, unshift, and splice for more on manipulating lists
# or ask a specific question here.

associative arrays (frequently called hashes) get a '%' sign. Hashes are simply
paired items, a key and value that goes with that key.

%name_age=('joe','35',
'jane','won't say',
'david','108',
'william',3'
);
# To see how old jane is....
$jane_age = $name_age{jane};

# As with the simple array dereferencing, the single element is refered to with '$'
# and not '%'. The {} braces identify the variable as a hash element.

# Perl dynamically mananges memory, so you can add and remove elements from # arrays at will.

$name_age{john} = 15; # added another key of 'john' with a value of '15'.

# or create new hashes as needed.... just do it..
$name_height{john} = '76';

# a new hash with one pair (john,76) now exists.

# david just had a birthday....
$name_age{david} = 109;

# old as dirt.... went from 108 to 109. Or, you could increment it like....
$name_age{david}++; # he just turned 110

You can combine arrays and array types to get just about any data structure you might want. Arrays of arrays of arrays....

'hope this helps





keep the rudder amid ship and beware the odd typo
 
You can have arrays of anything, including arrays. Don't try to define it like in C though. There is no static typing in Perl, so you can define your array &quot;my @array;&quot; and then set &quot;$array[1]{fred} = 10;&quot; and it will automatically transform the first element of your array into a hash and assign ten to key fred. Also, arrays act like lists in that they dynamically expand and contract automatically when you add or remove elements. There are even push() and pop() functions and shift() functions to treat arrays like stacks and queues. I quite like it. Check out and for more documentation and tutorials.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
hi tanderso,(anyone?)
can you elaborate a little and maybe give an example of
using an array of arrays? what i am trying to find out and
the books are not clear on this, is there a physical limitation on the number of dimensions one can specify in an array? i need up to 5 dimensions and not sure if PERL supports multi-dimensional(more than 2), or if arrays of arrays will 'work' for what i need. i am absolutely a PERL
newbie, just in the reading/researching stage. thanks.

CU
basilde
 
Example of array of arrays and hashes of arrays and hashes of hashes.... They can get as deep as you want to make them. The real limit is your ability to design, build, and maintain the memory structure. Perl will do what ever you ask.

Code:
# arrays of arrays
@a1 = ('a','b','c','d','e'); # a simple array
@a2 = ('1','2','3','4','5'); # another

# an array of references to two other arrays, @a1 and @a2.
@a3 = (\@a1,\@a2); 
print &quot;Array: $a3[1][2]\n&quot;;

# change the 3 in the second array to 6
$a3[1][2] = '6';
print &quot;Array - $a3[1][2]\n&quot;;

# grow the first embedded array by one element
$a3[0][5] = 'f';
print &quot;Added $a3[0][7]\n&quot;;

# a hash of arrays
$hash{'key1'} = \@a1;
$hash{'key2'} = \@a2;
print &quot;Hash Value - $hash{'key2'}[3]\n&quot;;

# change the '4' in @a2 to 7.
$hash{key2}[3] = '7';
print &quot;Hash Value - $hash{'key2'}[3]\n&quot;;

# the original arrays were changed.  See,
print &quot;a1: @a1\na2: @a2\n&quot;;

# hash of hashes
print &quot;\n\nHashes of hashes\n&quot;;
%families = (
    smith => { 
        husband => 'bill',
        wife    => 'sally',
        kid1    => 'bill, jr',
        kid2    => 'missy'
        },
    cleaver => {
        husband => 'ward',
        wife    => 'june',
        kid1    => 'wally',
        kid2    => 'beaver'
        },
    haskel => {
        kid1    => 'eddie',
        }
    );

# add a member to the smith family
$families{'smith'}{'kid3'} = 'bill, III';

# knock off beaver
delete $families{'cleaver'}{'kid2'};

foreach $k (keys (%families))
    {
    print &quot;family: $k \n&quot;;
    foreach $k2 (keys %{$families{$k}})
        {
        print &quot;\t$k2 - $families{$k}{$k2}\n&quot;;
        }
    }

# put beaver back and print the cleavers
print &quot;Cleavers, again:\n&quot;;
$families{cleaver}{kid2} = 'beaver';
foreach $k (keys %{ $families{'cleaver'}})
    {
    print &quot;\t$k $families{'cleaver'}{$k}\n&quot;;
    }

You can make them as big/deep as you want. 5 deep should be not problem for Perl. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top