Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Array length with range operator

by gurpreetsingh13 (Scribe)
on Jun 06, 2013 at 07:25 UTC ( #1037384=note: print w/replies, xml ) Need Help??


in reply to Re: Array length with range operator
in thread Array length with range operator

Thanks.

Good trick.

Anonymous array - then convert the ref. back to array and apply scalar to it.

Replies are listed 'Best First'.
Re^3: Array length with range operator
by AnomalousMonk (Canon) on Jun 06, 2013 at 08:27 UTC

    Also works for "highest index" rather than "number of elements":

    >perl -wMstrict -lE "say scalar $#{[ 'aa' .. 'zz' ]}; " 675

      Or insert commata and count them ;-)

      perl -wE "say 1 + join(',','aa' .. 'zz') =~ tr/,//;"

        But that's back to giving you the number of elements or, in general, something based on that number. De-referencing from  $#{[ LIST ]} gives you the highest index even if someone's been messing with  $[   (but Don't Do That!).

        Although why anyone would want to know the highest index of an array that no longer exists at the moment the datum is produced is beyond me — but then, the whole do-it-in-one-statement fixation escapes me, so...

        >perl -wMstrict -lE "say 1 + join(',','aa' .. 'zz') =~ tr/,//; ;; $[ = 2; say $#{[ 'aa' .. 'zz' ]}; " Use of assignment to $[ is deprecated at -e line 1. 676 677

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1037384]
help
Chatterbox?
[LanX]: good morning
[choroba]: LanX good indeed
[choroba]: very cloudy here, I planned to make some picrures, but now I don't think so
[LanX]: do you like it better now?
[choroba]: Well, getting used to it

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2017-02-26 04:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (371 votes). Check out past polls.