-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Mon, 19 Jul 2004 12:59, gohaku wrote:
> To get the length of an array, it's $#array, not [EMAIL PROTECTED] or #$array.
> Usually, I use scalar @array;
They do different things, afaik. $#array gives you the index of the last entry
in the array, scalar @array gives you the size. So in general,
[EMAIL PROTECTED]
> Problems with subroutines where the array is the first argument
> sub badsub()
> {
> my (@array,$scalar) = @_;
> #Pass Array last!
> #my ($scalar,@array) = @_:
> ...
> }
I got stuck for a while on this one. If you do this:
my @a = (7,8,9);
my $z = "hello";
badsub(@a, $z);
it is turned into:
badsub((7,8,9),"hello");
which, as Perl doesn't nest lists, and arguments are just lists, is:
badsub(7,8,9,"hello");
you see why it gets messy now when you try to access it?
There are a few ways around it:
badsub([EMAIL PROTECTED], $z); # ugly, but sometimes useful if you have more than
one list
you want to keep apart
badsub(@a, $z); # where badsub has a prototype: ([EMAIL PROTECTED])
badsub($z, @a); # better still
I wrote a little bit about working with references here:
http://www.kallisti.net.nz/PerlTips/ReferencesPassedToSubroutines
when I got stuck. However, I've since learnt more that I need to add to that
page. [Just done so]
> I still don't know how to declare arrays using only '$' instead of '@'
You can't, so far as I know. It doesn't make sense. @ signifies you want an
array. $ signifies you want a scalar. Look at it this way: if you want a
single value from something, prefix it with $. If you want a list, prefix it
with @. If you provide a list when something is expecting a single item, it
does 'scalar @list' which gives the size (someone correct me if I'm wrong
here, please :)
> anyway, Is it possible to write scripts using only '$' instead of other
> prefix symbols?
> In other words, a php-style script written in perl
PHP works with them quite differently. In that, a variable starts with $ and
may contain an array. It also doesn't have hashes like Perl (or rather, it
doesn't have arrays like Perl, they are kind of like a mix of the two,
similar to as if you didn't use arrays, but used hashs with '0', '1', '2' as
the key). Note, my PHP knowledge isn't all that good, so I may not be totally
accurate.
- --
Robin <[EMAIL PROTECTED]> JabberID: <[EMAIL PROTECTED]>
Hostes alienigeni me abduxerunt. Qui annus est?
PGP Key 0x776DB663 = DD10 5C62 1E29 A385 9866 0853 CD38 E07A 776D B663
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
iD8DBQFA+39EzTjgendttmMRAnOGAKCCEVl9ZkoP+sKSJP8VURdBJuMW+QCgln+L
tocP/YSbD3zRPtQEPnn+ARw=
=fxZM
-----END PGP SIGNATURE-----
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>