Ask a Question related to PERL Beginners, Design and Development.
-
Balaji thoguluva #1
Reg. length of anonymous array
Hi,
I am a novice to perl programming. I have a reference to a hash with a hash key as a reference to an anonymous array.
For clarity, I have something like this structure
my $rhash = {
$rarray => [ ],
};
I would like to know the length of the anonymous array containing some elements.
Thanks,
Balaji
Yahoo! India Education Special: Study in the UK now.
Balaji thoguluva Guest
-
associative array length
Is there any way of knowing the length of an associative array? I could build a static function that does the job (iterates over the as. array and... -
Array length problem
I seem to have a problem with the "array length". I can't seem to find any info as to what this refers to. Can anyone tell what this means. I am... -
Getting last element of anonymous array
I am trying to get the last element of the anonymous array below. Using $# I keep getting the 1st element (or so it seems anyway). What am I doing... -
#24897 [Com]: array_multisort() will reindex the array but not if array length is 1
ID: 24897 Comment by: franklin_se at hotmail dot com Reported By: chro at sokrates dot uio dot no Status: ... -
#24897 [Opn->Asn]: array_multisort() will reindex the array but not if array length is 1
ID: 24897 Updated by: sniper@php.net Reported By: chro at sokrates dot uio dot no -Status: Open +Status: ... -
David #2
Re: Reg. length of anonymous array
Balaji thoguluva wrote:
the usual trick use be used:> For clarity, I have something like this structure
>
> my $rhash = {
> $rarray => [ ],
> };
>
> I would like to know the length of the anonymous array containing some
> elements.
#!/usr/bin/perl -w
use strict;
my $h = {array => [1,3,5,7]};
print @{$h->{array}} . "\n";
print $#{$h->{array}} . "\n";
__END__
prints:
4
3
david
--
sub'_{print"@_ ";* \ = * __ ,\ & \}
sub'__{print"@_ ";* \ = * ___ ,\ & \}
sub'___{print"@_ ";* \ = * ____ ,\ & \}
sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)
David Guest
-
David #3
Re: Reg. length of anonymous array
Balaji,
next time when you reply please send it reply to the whole group, don't just
sent it to myself. this gives the others a chance to help you.
On Thursday 12 February 2004 20:05, you wrote:[snip]> Hi David,
>
> Thanks for your answer. It is working but I dont know what
> the dot operator signifies. I would appreciate if you could explain it
> or refer me to any documentation.
the dot operator does 2 things in this example:>> >
> > I would like to know the length of the anonymous array containing some
> > elements.
> the usual trick use be used:
>
> #!/usr/bin/perl -w
> use strict;
>
> my $h = {array => [1,3,5,7]};
>
> print @{$h->{array}} . "\n";
> print $#{$h->{array}} . "\n";
>
1. it concatenates the two operants and creates a string out of them.
2. it forces both operants to be in scalar context.
the second point is important, consider:
print @{$h->{array}},"\n";
print $#{$h->{array}},"\n";
without the dot operator, it prints:
1357
3
because @{$h->{array}} is taken in list context by the 'print' function.
david
--
sub'_{print"@_ ";* \ = * __ ,\ & \}
sub'__{print"@_ ";* \ = * ___ ,\ & \}
sub'___{print"@_ ";* \ = * ____ ,\ & \}
sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)
David Guest
-
Shiping #4
Re: Reg. length of anonymous array
Hi,
How can I rearrange an array in a specific order based on the order of a
hash? Something like this:
my @a = qw(Mary John Dan);
print join "\t", @a, "\n";
my %b = ( John => 0,
Dan => 1,
Mary => 2);
print "$_ => $b{$_}\n" for (keys %b);
print "$_-$b{$_}\t" foreach sort {$b{$a} <=> $b{$b}} keys %b;
The final order for @a expect:
John Dan Mary
Thanks,
Shiping
Shiping Guest



Reply With Quote

