To get a variable’s length, we can use the .length
property in D. However, this doesn’t offer the luxury of comparing different variables by length.
Therefore, the isSameLength()
function is used. It compares the length of two variables and returns true
or false
. It returns true
if both lengths are the same. Otherwise, it returns false
.
isSameLength(var_a,var_b)
var_a
: This is the first variable whose length is to be checked.var_b
: This is the second variable whose length is up for comparison.import std.stdio;import std.algorithm;//start main functionvoid main(){//declare variable to compare lengthint [] var_a = [1,2,3,4,5];int [] var_b = [6,7,8,9,10];string var_c = "This is the same";//comparing length of var_a and var_bif(isSameLength(var_a,var_b)){writeln("They are equal");}//comparing length of var_a and var_cif(isSameLength(var_a,var_c)){writeln("They are equal");}else{writeln("They are not equal");}}
main
function.var_a
and var_b
using the isSameLength()
method inside an if
condition.var_a
and var_c
using the isSameLength()
method inside an if
, else
condition.