PERL array of arrays

B

BKennedy

I've done some reading and I'm having difficulties implementing an
'array of arrays'. I've made a random number generator that places 10
numbers (0 to 100) into an 10 element array(A). I'm now trying to
make and print back another 10 element array (B) that contains 10 of
the first arrays (A) (I don't want to flatten each array (A) into a
single array (B)).
Example:
1 74 45 23 ...etc
34 15 33 0 ...etc
23 44 11 5 ...etc

Any examples are appreciated. Thanks in advance.
 
D

David K. Wall

BKennedy said:
I've done some reading and I'm having difficulties implementing an
'array of arrays'.

Have you looked at 'perldoc perllol' ? That has a number of examples
that should get you started.
 
S

Schlick

Tad said:
Did it include:

perldoc perllol

??

Perl Laughs Out Loud? Perl Lots of Love?

Oh, right - Perl Lists of Lists.

How could someone who didn't know exactly where to look possibly
overlook _that_?

Now for some actual help for the original poster...

# Sub for making list 'A'
sub make_random_list {
# Push random numbers onto an array reference
my $ret = [];
# I hate $_ - I don't use $i, but there
# are no possible side-effects
for my $i (0..9) {
push @$ret, int(rand(101));
}
return $ret;
}

# Sub for making list 'B' - list of lists
sub make_random_lol {
my $ret = [];
# Use make_random_list to make an array ref
# and then push that ref onto another array
# (also accessed by a reference)
for my $i (0..9) {
push @$ret, make_random_list();
}
return $ret;
}

# Sub for printing list 'B'
sub print_random_lol {
my $lol = make_random_lol();
my $i = 0;
# Loop through the 'outer' list
for my $l (@$lol) {
# $l is a list - pretty print it
# using join
print "List $i: [", join(", ", @$l), "]\n";
++$i;
}
}

Tad is right though - the perldocs contain a wealth of information. The
problem is that the 'index' is absolutely woeful. You either need to
know the name of the function you want to use, an exact question or word
from a question (but don't use "email" - it won't work unless you use
the faq term "mail") or the summary name that has been given to a
certain section, eg 'perllol', 'perldsc', 'perltooc' etc.

BTW - guess what 'perlbot' describes. Hint: it has nothing to do with
Bots or the bottom of anything.

The very first thing you should do is:

perldoc perl

This gives you the 'index', but also a description of what's in each of
the docs. The description is far and away the most useful part of
perldoc's 'index'.

Quoting "You obviously didn't read manual 4a chapter 6 section 12
subsection 2 paragraph 3 line 1" isn't all that handy.
 
T

Tad McClellan

Schlick said:
Perl Laughs Out Loud? Perl Lots of Love?

Oh, right - Perl Lists of Lists.

How could someone who didn't know exactly where to look possibly
overlook _that_?


I did say or imply that he overlooked that.

It looked to me like he did not know about it.

I told him about it.
 
T

Tad McClellan

the perldocs contain a wealth of information. The
problem is that the 'index' is absolutely woeful.


Perl is open source.

It is improved by volunteers.

If you see something wrong with Perl, pitch in to help fix it.

(or are you suggesting that "someone else" do the work you want done?)

Let us know when you have the new and improved index ready...
 
A

Anno Siegel

Schlick said:
Perl Laughs Out Loud? Perl Lots of Love?

Oh, right - Perl Lists of Lists.

How could someone who didn't know exactly where to look possibly
overlook _that_?

Now for some actual help for the original poster...

Finally! Someone who knows how to run a newsgroup.
# Sub for making list 'A'
sub make_random_list {
# Push random numbers onto an array reference
my $ret = [];
# I hate $_ - I don't use $i, but there
# are no possible side-effects

Why introduce the name when you don't use it?
for my $i (0..9) {
push @$ret, int(rand(101));
}
return $ret;
}

That's unnecessarily complicated. Perl has functions (like map()),
that manipulate lists as a whole:

sub make_random_list { [ map int rand 101, 1 .. 10] }
# Sub for making list 'B' - list of lists
sub make_random_lol {
my $ret = [];
# Use make_random_list to make an array ref
# and then push that ref onto another array
# (also accessed by a reference)
for my $i (0..9) {
push @$ret, make_random_list();
}
return $ret;
}

Similarly:

sub make_random_lol { [ map make_random_list(), 1 .. 10] }
# Sub for printing list 'B'
sub print_random_lol {
my $lol = make_random_lol();
my $i = 0;
# Loop through the 'outer' list
for my $l (@$lol) {
# $l is a list - pretty print it
# using join
print "List $i: [", join(", ", @$l), "]\n";
++$i;
}
}

sub print_random_lol {
local $, = ', ';
my $i = 0;
print "List @{[ $i++]}: [@$_]\n" for @{ make_random_lol()};
}

Anno
 
B

BKennedy

Schlick said:
Perl Laughs Out Loud? Perl Lots of Love?

Oh, right - Perl Lists of Lists.

How could someone who didn't know exactly where to look possibly
overlook _that_?
Quoting "You obviously didn't read manual 4a chapter 6 section 12
subsection 2 paragraph 3 line 1" isn't all that handy.


Thank you for the cryptic puzzle you've given me . . . everything i
dont understand at least gives me a direction to start learning in.
And here I thought ppl were laughing at me (perllol). Thanks again.
-Bkennedy
 
S

Schlick

Anno Siegel wrote:
# Sub for making list 'A'
sub make_random_list {
# Push random numbers onto an array reference
my $ret = [];
# I hate $_ - I don't use $i, but there
# are no possible side-effects


Why introduce the name when you don't use it?

That's just my own personal thing. I hate $_ with a passion (although I
have to use it with map/grep etc). I've seen a few too many $_ traps so
I steer clear of the whole country.
That's unnecessarily complicated. Perl has functions (like map()),
that manipulate lists as a whole:

sub make_random_list { [ map int rand 101, 1 .. 10] }

Definitely - but although it's functionally more complicated, it's
easier for someone who's learning how to do these things to read.
Normally I wouldn't even have the two subs for making the list-of-lists :)
sub print_random_lol {
local $, = ', ';
my $i = 0;
print "List @{[ $i++]}: [@$_]\n" for @{ make_random_lol()};
}

Now you're just asking for a slew of questions about what $, is :)

Your solution is much more concise and clean, but I didn't want to
assume too much knowledge about some of the more complex perl trickery.
The good thing is that now the original poster has simplistic-v-advanced
code to compare and learn from.
 
A

Anno Siegel

Schlick said:
Anno Siegel wrote:
# Sub for making list 'A'
sub make_random_list {
# Push random numbers onto an array reference
my $ret = [];
# I hate $_ - I don't use $i, but there
# are no possible side-effects


Why introduce the name when you don't use it?

That's just my own personal thing. I hate $_ with a passion (although I
have to use it with map/grep etc). I've seen a few too many $_ traps so
I steer clear of the whole country.

That is your reason for not using "$_". Why did you introduce "$i"?

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top