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] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to