Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Re: Nearest Neighbour Analysis subroutine

by Elias (Pilgrim)
on Jan 15, 2001 at 22:02 UTC ( [id://51979]=note: print w/replies, xml ) Need Help??


in reply to Re: Nearest Neighbour Analysis subroutine
in thread Nearest Neighbour Analysis subroutine

I thought I had understood your comment, but there is still something I can't figure out. Why does:
my @test=qw(1 2 3 4 5 6); (@test % 2) ? print "uneven\n" : print "even\n"; print "end\n";

Work fine, while the two versions below result in "uneven" in the first case and "even" in the second, when both should be reported as "even".

@test=qw(1 2 3 4 5 6); testthearray1(\@test); sub testthearray1{ (@_ % 2) ? print "uneven\n" : print "even\n"; } testthearray2(); sub testthearray2{ (@_ % 2) ? print "uneven\n" : print "even\n"; }

The BlueCamel says "Any arguments passed to a Perl routine come in as the array @_" (pg 219), and this is clearly what you assume happens when you assign my @mat = @_ in the second line. When I include something like  print "@_" to the second line in the  sub testhearray (1 and 2), it shows up as an undefined array (or rather, ARRAY(0x9b25fd8) in the first, and nothing in the second).

I also find it strange that it is the first of the two subs that apparently requires a reference to @test instead of @_, when this is the one which includes a reference to the array when calling the subroutine.

What am I overlooking? (this humbled novice asks).

Replies are listed 'Best First'.
Re: Re: Re: Nearest Neighbour Analysis subroutine
by danger (Priest) on Jan 15, 2001 at 22:12 UTC

    Elias if you pass a reference to an array then @_ receives that reference -- that is, one element (no matter how big the array is). In testthearray1(\@test) you are only passing in one element. In testthearray2() you aren't passing in anything at all.

    The routine I showed should just be called as:

    my @array = (1,2,3,4,5,6); my $ndist_sum = nneighbours(@array);
Re: Re: Re: Nearest Neighbour Analysis subroutine
by merlyn (Sage) on Jan 15, 2001 at 22:09 UTC
    @test=qw(1 2 3 4 5 6); testthearray1(\@test); sub testthearray1{ (@_ % 2) ? print "uneven\n" : print "even\n"; }
    You're passing one thing, all the time. That's always odd (and odd!). Remove the \ in front of @test.

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://51979]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-24 09:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found