Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

strange syntax

by grasshopper!!! (Beadle)
on Jun 16, 2015 at 22:11 UTC ( [id://1130720]=perlquestion: print w/replies, xml ) Need Help??

grasshopper!!! has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone explain the $#kbin notation in the loop below. When I assign a scalar a string and print scalar with inserted # I get -1

$ans = 1; # $kbin = split(/./,unpack('B*',pack('H*',$k))); # for ($i=0; $i<$#kbin; i++) # { # $ans = $ans * $ans % $N; # if (substr($kbin,$i,$1) == 1) { $ans = $ans * $M % $N +; } # }

Replies are listed 'Best First'.
Re: strange syntax
by stevieb (Canon) on Jun 16, 2015 at 22:27 UTC

    That funny syntax represents the index of the last element of an array starting from 0, eg:

    #!/usr/bin/perl use warnings; use strict; my @a = qw(0 1 2 3 4 5); print "$#a\n\n"; for my $i (0 .. $#a){ print "$i\n"; } print "elem 5: $a[$#a]\n"; # same as $a[-1] or $a[5]

    Output:

    5 0 1 2 3 4 5 elem 5: 5

    -stevieb

      Slight nit, even if it is not recommended use (deprecated since 5.12.0):

      That funny syntax represents the index of the last element of an array starting from 0$[
      Today 0 is accurate, but who knows the original context of the provided code.

      --MidLifeXis

Re: strange syntax
by hexcoder (Curate) on Jun 16, 2015 at 22:30 UTC
    Hello,

    $#kbin gives the index of the last entry of array @kbin. Since @kbin is undefined, it is empty, and so the result is -1. 0 would be the first index of an non empty array.

    If you use $#{$kbin}, you should get the index of the last entry of @{$kbin}, that is the array dereferenced by $kbin.

      On the same lines if last-index-of-array variable, $#x, is assigned -1, array becomes empty (mentioned in perldata) ...

      DB<1> @x = ( 1 , 2 ) DB<2> x @x 0 1 1 2 DB<3> $#x = -1 DB<4> x @x empty array DB<5>

      Thank you for answering can you explain why $kbin = split( is used and not @kbin = split(

        That's probably just a mistake. Calling split in scalar context will assign the number of items produced by the split to the scalar, so $kbin will contain the number of items the split returns, not the items themselves. That might make sense in some cases, but not here, since $kbin is being used as if it's an array later.

        My first guess would be that this was written by a PHP programmer, since their arrays don't use a different sigil like Perl's do.

        Aaron B.
        Available for small or large Perl jobs and *nix system administration; see my home node.

Re: strange syntax
by toolic (Bishop) on Jun 16, 2015 at 23:25 UTC
    The documentation explains it: perldata ... search for $#

      The documentation explains it: perldata ... search for $#

      Let see what all explain it [perldoc://"$#"] "$#" -> perlref   $aref->$#*; # same as $#{ $aref }

      Wow, what a horrible search engine, that it five pages of results

       [ddg://"$#" site:perldoc.perl.org] "$#" site:perldoc.perl.org -> more of same nonsense, wow

      hello http://grep.cpan.me/?q=\%24%23\w%2B+dist%3Aperl+ ->

      The Perl equivalent for this is C<$#myarray>. Set the highest index in the array to the given number, equivalent to Perl's C<$#array = $fill;>. RJ

      hello http://grep.cpan.me/?q=\%24%23\w%2B%20dist%3Aperl%20+dist=perl

      trigger for I<container magics>, i.e. it will for C<%ENV> or C<%SIG> but not for C<$#array>. perl-5.22.0/pod/perldata.pod $days{'Feb'} # the 'Feb' value from hash %days $#days # the last index of array @days The length of an array is a scalar value. You may find the length of array @days by evaluating C<$#days>, as in B<csh>. However, this isn't the length of the array; it's the subscript of the last elemen which is a different value since there is ordinarily a 0th element. Assigning to C<$#days> actually changes the length of the array. Shortening an array this way destroys intervening values

      hello http://search.cpan.org/grep?cpanid=RJBS&release=perl-5.22.0&string=\%24%23&i=1&n=1&C=0#pod/perldata.pod

      pod/perldata.pod 53: $#days # the last index of array @days 357:of array @days by evaluating C<$#days>, as in B<csh>. However, th +is 360:Assigning to C<$#days> actually changes the length of the array. 364:X<$#> X<array, length> 373: $#whatever = -1; 382: scalar(@whatever) == $#whatever + 1;

        Or one could, as toolic suggests, actually look at perldata and search for the string '$#', which yields

        The length of an array is a scalar value. You may find the length of
        array @days by evaluating $#days, as in csh. However, this isn't the
        length of the array; it's the subscript of the last element ...
        as the second hit, followed by a few more informative paragraphs.


        Give a man a fish:  <%-(-(-(-<

Re: strange syntax ( ppi_dumper! )
by Anonymous Monk on Jun 16, 2015 at 23:17 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-24 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found