scalar keyword in Perl is used to convert the expression to scalar context. This is a forceful evaluation of expression to scalar context even if it works well in list context.
Syntax: scalar expr
Returns: a scalar value
Example 1:
Perl
#!/usr/bin/perl -w
@array1 = ("Geeks", "For", "Geeks");
@array2 = (1, 1, 0, 0, 9, 6);
@array3 = (@array1, @array2);
print "Array in List form: @array3\n";
@array3 = (scalar(@array1), scalar(@array2));
print "Array in scalar form: @array3\n";
|
Output:
Array in List form: Geeks For Geeks 1 1 0 0 9 6
Array in scalar form: 3 6
Example 2:
Perl
#!/usr/bin/perl -w
@array1 = ("Welcome", "To", "Geeks");
@array2 = (1, 1, 0, 0, 9, 6);
@array3 = ( @array1, @array2 );
print "Concatenation of Arrays: @array3\n";
@array3 = (scalar(@array2) - scalar(@array1));
print "Difference in number of elements: @array3\n";
|
Output:
Concatenation of Arrays: Welcome To Geeks 1 1 0 0 9 6
Difference in number of elements: 3
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Aug, 2021
Like Article
Save Article