gurpreetsingh13 has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
I believe this must be something very simple..might be I am missing something.
For Array length, we use either $#<array-name> or scalar(<array>) or something with print to force a scalar context.
How can we create runtime arrays using range operator and get immediate length of the same.
Tried a few things and searched too, but can't find the solution.
This works-This also works-perl -e 'my @a="aa".."zz"; print "Length=".@a;'
This also works-perl -e 'my @a="aa".."zz"; print scalar(@a);'
perl -e 'my @a="aa".."zz"; print @#a;'
Just if I need to do it in a single statement..
Syntax error thrown-
perl -e 'print length($("aa".."zz"));'
Syntax error thrown
perl -e 'print length(@("aa".."zz"));'
Even if I force scalar context,syntax error thrown.
perl -e 'my $len=@("aa".."zz");print $len;'
Might be I am forgetting something basic or so. Need your help please.
PS: Please don't advice on strict and warnings etc. This is just something quick and dirty.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Array length with range operator
by Happy-the-monk (Canon) on Jun 06, 2013 at 06:39 UTC | |
by gurpreetsingh13 (Scribe) on Jun 06, 2013 at 07:25 UTC | |
by AnomalousMonk (Archbishop) on Jun 06, 2013 at 08:27 UTC | |
by hdb (Monsignor) on Jun 06, 2013 at 09:35 UTC | |
by AnomalousMonk (Archbishop) on Jun 06, 2013 at 19:04 UTC | |
| |
Re: Array length with range operator
by Athanasius (Archbishop) on Jun 06, 2013 at 06:36 UTC | |
by gurpreetsingh13 (Scribe) on Jun 06, 2013 at 07:27 UTC | |
by hdb (Monsignor) on Jun 06, 2013 at 09:19 UTC | |
Re: Array length with range operator
by kcott (Archbishop) on Jun 06, 2013 at 06:42 UTC | |
Re: Array length with range operator
by hbm (Hermit) on Jun 07, 2013 at 19:05 UTC |
Back to
Seekers of Perl Wisdom