RE: Getting the length of an array that is a reference...
Hamish Whittal said: > > I have a reference to an array $ref_arr > > I want the length of the array. normally just getting the length one > would write something along the lines of: > $length = @#array; # Unless I am mistaken. Close -- you want $#array instead. > > How do I find the length if I have a reference? > You can get the index of the last element with $#$ref_arr, and the length with scalar(@$ref_arr). Try this code: #! perl -w use strict; my @array = ( 0, 1, 1, 2, 3, 5, 8 ); my $ref_arr = \@array; print "For array \@array:\n", 'Length of array: ', scalar( @array ) , "\n", 'Index of last element: ', $#array , "\n", "\n", "For array referenced via \$ref_arr:\n", 'Length of array: ' , scalar( @$ref_arr ) , "\n", 'Index of last element: ' , $#$ref_arr , "\n", "\n"; __END__ --Bill -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Getting the length of an array that is a reference...
On Fri, Aug 31, 2001 at 08:02:32PM +0530, [EMAIL PROTECTED] wrote: > You can also use $#{$array} to get the length.. Except that is not the length, unless you've messed with $[, which you haven't, right? perldoc perlvar for more info on $[ - but read the whole entry. -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Getting the length of an array that is a reference...
> "Aravindan" == Aravindan Sundaram <[EMAIL PROTECTED]> writes: Aravindan> You can also use $#{$array} to get the length.. No, that gets the highest index. One short of the length. Aravindan> Aravindan> This electronic mail message is intended solely for the named recipients Aravindan> and may contain confidential and proprietary business information of eFunds Aravindan> Corporation and all its subsidiaries. If you are not a named recipient, Aravindan> please notify the sender immediately. You may not disclose the contents to Aravindan> any other person; use this electronic mail message or its contents for any Aravindan> other purpose; or further store or copy its contents in any medium Aravindan> You have exceeded the 4-line .sig boilerplate limit with a worthless unenforcable disclaimer. Please remove this text from future postings to this mailing list. If you cannot do so for mail from your domain, please get a freemail account and rejoin the list from there. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Getting the length of an array that is a reference...
On Fri, 31 Aug 2001 [EMAIL PROTECTED] wrote: > You can also use $#{$array} to get the length.. No, that only gives you the index of the last element! You will be off by one if you do it that way. The correct way is to put the array (or array ref) into a scalar context, which gives the length of the array. -- Brett http://www.chapelperilous.net/ Punishment becomes ineffective after a certain point. Men become insensitive. -- Eneg, "Patterns of Force", stardate 2534.7 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Getting the length of an array that is a reference...
You can also use $#{$array} to get the length.. Hamish Whittal To: perl beginners <[EMAIL PROTECTED]> Sent by: cc: hamish Subject: Getting the length of an array that is a reference... 08/31/01 08:54 PM Please respond to hamish Hi, I have a reference to an array $ref_arr I want the length of the array. normally just getting the length one would write something along the lines of: $length = @#array; # Unless I am mistaken. How do I find the length if I have a reference? Cheers Hamish -- Hamish Whittal QED Technologies Tel: +27 21 448 9291 [EMAIL PROTECTED] Fax: +27 21 448 9551 `The' Linux Services Company Cel: +27 82 803 5533 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This electronic mail message is intended solely for the named recipients and may contain confidential and proprietary business information of eFunds Corporation and all its subsidiaries. If you are not a named recipient, please notify the sender immediately. You may not disclose the contents to any other person; use this electronic mail message or its contents for any other purpose; or further store or copy its contents in any medium -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Getting the length of an array that is a reference...
You can always do this scalar @$ref_arr but this will give you the number of elements not the last index as $#array would On Fri, Aug 31, 2001 at 01:24:53PM -0200, Hamish Whittal shaped the electrons to read: > Hi, > > I have a reference to an array $ref_arr > > I want the length of the array. normally just getting the length one > would write something along the lines of: > $length = @#array; # Unless I am mistaken. > > How do I find the length if I have a reference? > > Cheers > Hamish > -- > Hamish WhittalQED TechnologiesTel: +27 21 448 9291 > [EMAIL PROTECTED]Fax: +27 21 448 9551 > `The' Linux Services CompanyCel: +27 82 803 5533 > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Getting the length of an array that is a reference...
> -Original Message- > From: Brett W. McCoy [mailto:[EMAIL PROTECTED]] > Sent: Friday, August 31, 2001 8:24 AM > To: Hamish Whittal > Cc: perl beginners > Subject: Re: Getting the length of an array that is a reference... > > > On Fri, 31 Aug 2001, Hamish Whittal wrote: > > > I want the length of the array. normally just getting the length one > > would write something along the lines of: > > $length = @#array; # Unless I am mistaken. > > That's the incorrect way to get the length of the array > anyway -- you are > only getting the index of the last element. Actually, @#array is a syntax error. The index of the last element would be $#array. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Getting the length of an array that is a reference...
On Fri, 31 Aug 2001, Hamish Whittal wrote: > I want the length of the array. normally just getting the length one > would write something along the lines of: > $length = @#array; # Unless I am mistaken. That's the incorrect way to get the length of the array anyway -- you are only getting the index of the last element. If you put the array into a scalar context, you get the length: my $length = @array; > How do I find the length if I have a reference? my $length = @{$arrayref}; -- Brett http://www.chapelperilous.net/ There is no statute of limitations on stupidity. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Getting the length of an array that is a reference...
you'll need to dereference it.. this would work for you: perl -e "$ref=[qw(1 2 3)]; print scalar @$ref" regards, Jos Boumans - Original Message - From: "Hamish Whittal" <[EMAIL PROTECTED]> To: "perl beginners" <[EMAIL PROTECTED]> Sent: Friday, August 31, 2001 5:24 PM Subject: Getting the length of an array that is a reference... > Hi, > > I have a reference to an array $ref_arr > > I want the length of the array. normally just getting the length one > would write something along the lines of: > $length = @#array; # Unless I am mistaken. > > How do I find the length if I have a reference? > > Cheers > Hamish > -- > Hamish Whittal QED Technologies Tel: +27 21 448 9291 > [EMAIL PROTECTED] Fax: +27 21 448 9551 > `The' Linux Services Company Cel: +27 82 803 5533 > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Getting the length of an array that is a reference...
Hi, I have a reference to an array $ref_arr I want the length of the array. normally just getting the length one would write something along the lines of: $length = @#array; # Unless I am mistaken. How do I find the length if I have a reference? Cheers Hamish -- Hamish Whittal QED TechnologiesTel: +27 21 448 9291 [EMAIL PROTECTED] Fax: +27 21 448 9551 `The' Linux Services CompanyCel: +27 82 803 5533 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: length of an array
On Tue, 7 Aug 2001, Romek KrisztiƔn wrote: > @array[$a] = [1, 2, 3, 4, 5]; Should be $array[$a] = [ 1, 2, 3, 4, 5 ]; > How can I determine the length of an array inside an array? > > I tried the followings: > > $length = $#array[$a]# --> syntax error > $length = scalar(@array[$a]) # --> ARRAY(blablabla) $length = @{ $array[$a] }; -- Brett http://www.chapelperilous.net/btfwk/ Only that in you which is me can hear what I'm saying. -- Baba Ram Dass -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
length of an array
Hello! @array[$a] = [1, 2, 3, 4, 5]; How can I determine the length of an array inside an array? I tried the followings: $length = $#array[$a]# --> syntax error $length = scalar(@array[$a]) # --> ARRAY(blablabla) Any ideas? Krisztian -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]