First part:
You get the size of an array by using it in a scalar context.
Sounds complicated?
Scalar context means that the left side of an assignment is a
scalar. So you just write:
$size = @a;
and you get the size of the array @a.
Another method would be to get the index of the last element and
add 1. The index of the last element in an array is $#arrayname.
This gives:
$size = $#a + 1;
to get the size of array @a
Part two:
I like the Book Advanced Perl Programming (the Panther-Book from
O'Reilly).
It has a couple of sections about perls guts. Speeking of which: you
should
try
perldoc perlguts
to get started.
hope this helps,
cr
On Mon, 14 May 2001 12:59:54 -0400 (EDT), Bryce Tate said:
> Hi, I am fairly new to perl, and have a few questions about a small
> program I’m writing. This program is reading from a text file that is
> delimited by colons. Eg...
> Field One:Field Two:Field Three:Field Four
> I know the first couple of fields are specific and will always be there, but
> after that, the number of fields per line will vary. So to handle this I
> used split like so...
> ($Q[$i], $numOfAns, $correct, @A) = split(/:/);
> So that the first entry is put into a particular place in an array and the
> next two in separate variables and then everything else in the A array.
> What I need to do next is print out some information in the array in order
> with some other information, which I have working, but to do this I have to
> use a control loop whose condition is
> while($ansNum < $numOfAns)
> where $ansNum starts at zero and is incremented until it is at
> $numOfAns(from the split line above, it’s value should represent the number
> of elements in @A). This all works, but I would like to improve it and make
> it so that the user making the text file
> doesn’t have to enter, $numOfAns, and that instead I could find out the size
> of the @A array.
>
> I think I could do this by looping and checking for a “” entry while
> counting, but that could not work in the case when the file has a “::” in
> it. Another way I think I might be able to do this would be to append a
> NULL entry on the end and loop/count to that(Is this the “undef” value in
> perl, and if so what literal am I testing for). Or maybe Perl has some neat
> automatic counter I’m unaware of(as I am new to perl and am finding the
> concepts of not needing to declare the size of
> an array and not having to specify types rather odd(but nice to program
> with).
>
> Also, with that last point, what book/perldoc should I read if I want to
> learn about some of the internals of Perl...like the memory structure of
> variables/arrays and how they are dealt with
> internally? If they aren’t structurally dissimilar from C++’s then the
> implementation in perl must be way different and I’d be interested in
> learning about the advantages/drawbacks(overhead, etc.)
>
> Thanks for any/all the help,
>
> Bryce Tate
>
>
> ______________________________________________
> FREE Personalized Email at Mail.com
> Sign up at http://www.mail.com/?sr=signup
>
>