Re: Another Perl datatype headache ( scalars $, hashes %, and arrays @ )

2004-07-20 Thread gohaku
On Jul 19, 2004, at 9:46 AM, James Edward Gray II wrote:
anyway, Is it possible to write scripts using only '$' instead of 
other prefix symbols?
In other words, a php-style script written in perl
Whether it is or not, it's not the answer to your problems.
Start trying to make sense of the world you find yourself in.  There 
are rules.  Try to understand why things are happening, not just that 
they are happening.

$ is for scalars.  Even with $hash{some_key} and $array[0] we're 
talking about one entry of the group, a scalar.

...
% is similar, for the hash as a whole.  For example, we don't want the 
keys() of one entry of a hash, that doesn't make sense.  We want the 
keys() of an entire hash, so we call it with:

keys %some_hash

Hopefully some of this makes sense and helps get you over the hump.  
Hang in there.

James

Thanks a lot for your advice James, I found what you said, especially:
Start trying to make sense of the world you find yourself in.  There 
are rules.  Try to understand why things are happening, not just that 
they are happening.
to be encouraging, yet thought-provoking.
I'll have to keep the following quote in mind:
"Without rules, there is chaos" ~ anonymous
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: Another Perl datatype headache ( scalars $, hashes %, and arrays @ )

2004-07-19 Thread James Edward Gray II
On Jul 18, 2004, at 7:59 PM, gohaku wrote:
Hi everyone,
Howdy.
after writing perl scripts for about 3 years now, I still have trouble 
with the
basic datatypes.
I know that variables that start with '$' are scalars.
This covers Hashes ($HASH{$key}), Arrays ( $_[0] ), and
regular scalar values ( $foobar );
Looks like you are doing good to me.
The code I write as well other's code is still unreadable to me
even though I have followed examples from the
Camel book, many other Perl books from O'Reilly and online references.
I have also used perldoc on many occasions.
I'm sorry to hear that.  Sounds like you are doing most of the right 
things.  Perhaps you just need to give it some more time.

There are still some things that haven't sunk in, such as:
If I want to add Hash keys to another Hash, I do the following:
%HASH = ( 1 => 'one' ); #NO BRACES OR ELSE
%HASH2 = ( 2 => 'two' ); AGAIN, NO BRACES OR ELSE...
@HASH2{ keys %HASH } = "";
#confusing, considering it's the symbol used for arrays
If it's confusing, let's do it a different way.
for my $key (keys %HASH) {
$HASH{$key} = "";
}
Shorter isn't always better.  Use what you can understand.
To get the length of an array, it's $#array, not [EMAIL PROTECTED] or #$array.
Usually, I use scalar @array;
$#array is not the length of the array.  It is the number (#) of the 
last scalar ($) held in the array or more simply, the last index that 
has been assigned.

scalar(@array) is the length and the way to go, so again I see no 
problem here.

Problems with subroutines where the array is the first argument
sub badsub()
{  
 
my (@array,$scalar) = @_;   
#Pass Array last!
#my ($scalar,@array) = @_:
...
}
Again, you show the fix for the problem you mention.  Looks like you 
understand a lot more than you give yourself credit for.

If you are sure your sub is called:
some_sub( @array, $scalar )
You could use:
sub some_sub {
my $scalar = pop @_;
my @array = @_;
# ...
}
But I much prefer reversing the order, as you did above.
I still don't know how to declare arrays using only '$' instead of '@'
I believe you are talking about references here.  They would be a good 
solution to the problem above.  Using them you can pass 5 arrays, 3 
hashes and 2 scalars to a sub in any order you like.

However, they complicate things a little.  If you're still having 
trouble grasping Perl without them, give it a little more time before 
running down that road.

anyway, Is it possible to write scripts using only '$' instead of 
other prefix symbols?
In other words, a php-style script written in perl
Whether it is or not, it's not the answer to your problems.
Start trying to make sense of the world you find yourself in.  There 
are rules.  Try to understand why things are happening, not just that 
they are happening.

$ is for scalars.  Even with $hash{some_key} and $array[0] we're 
talking about one entry of the group, a scalar.

@ is for when we are talking about an array as a whole:
my @array  # create array
scalar @array  # get length of array
@array = @_  # copy array
% is similar, for the hash as a whole.  For example, we don't want the 
keys() of one entry of a hash, that doesn't make sense.  We want the 
keys() of an entire hash, so we call it with:

keys %some_hash
With sub routines, everything passed is folded into an array @_.  We do 
that so subroutines can handle varying numbers of parameters with ease. 
 Because of that, if we pass an array and a scalar, they are going to 
end up in @_ together and we need to separate them back out on the 
inside.  We've seen different ways to do that above, and references 
provide yet another way.

Hopefully some of this makes sense and helps get you over the hump.  
Hang in there.

James
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: Another Perl datatype headache ( scalars $, hashes %, and arrays @ )

2004-07-19 Thread Robin
-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]
 




Another Perl datatype headache ( scalars $, hashes %, and arrays @ )

2004-07-18 Thread gohaku
Hi everyone,
after writing perl scripts for about 3 years now, I still have trouble 
with the
basic datatypes.
I know that variables that start with '$' are scalars.
This covers Hashes ($HASH{$key}), Arrays ( $_[0] ), and
regular scalar values ( $foobar );

The code I write as well other's code is still unreadable to me
even though I have followed examples from the
Camel book, many other Perl books from O'Reilly and online references.
I have also used perldoc on many occasions.
There are still some things that haven't sunk in, such as:
If I want to add Hash keys to another Hash, I do the following:
%HASH = ( 1 => 'one' ); #NO BRACES OR ELSE
%HASH2 = ( 2 => 'two' ); AGAIN, NO BRACES OR ELSE...
@HASH2{ keys %HASH } = "";
#confusing, considering it's the symbol used for arrays
To get the length of an array, it's $#array, not [EMAIL PROTECTED] or #$array.
Usually, I use scalar @array;
Problems with subroutines where the array is the first argument
sub badsub()
{  
 
my (@array,$scalar) = @_;   
#Pass Array last!
#my ($scalar,@array) = @_:
...
}
I still don't know how to declare arrays using only '$' instead of '@'
anyway, Is it possible to write scripts using only '$' instead of other 
prefix symbols?
In other words, a php-style script written in perl

Thanks in advance.
-gohaku
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]