Perl Sequential Number Arrays

PERLServer Side ProgrammingProgramming Scripts

Perl offers a shortcut for sequential numbers and letters. Rather than typing out each element when counting to 100 for example, we can do something like as follows −

Example

 Live Demo

#!/usr/bin/perl
@var_10 = (1..10);
@var_20 = (10..20);
@var_abc = (a..z);
print "@var_10\n";  # Prints number from 1 to 10
print "@var_20\n";  # Prints number from 10 to 20
print "@var_abc\n"; # Prints number from a to z

Here double dot (..) is called range operator. This will produce the following result −

1 2 3 4 5 6 7 8 9 10
10 11 12 13 14 15 16 17 18 19 20
a b c d e f g h i j k l m n o p q r s t u v w x y z
raja
Published on 28-Nov-2019 09:38:57
Advertisements