in reply to Length of strings in an array

Hello shabird,

it gives the length of the string but it also counts the enter key

You should chomp the string to remove the trailing newline before finding the length.

the order is not sorted although i have used to sort function for that.

By default, sort sorts in lexicographical order, so 100 comes before 90 because 1 comes before 9. To sort numerically, do this:

my @array = sort { $a <=> $b } $frstLength, $scndLength, $trdLength, $ +frthLength;

You should also begin your script with use strict;, which forces you to declare each variable with my. (This is a good thing! It will save you a lot of debugging time in the long run.)

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Length of strings in an array
by shabird (Sexton) on Sep 20, 2020 at 16:53 UTC

    Hello Athanasius Thank you for your explanation it helped but can you please explain what does this code { $a <=> $b } means?

      Athanasius uses the sort BLOCK LIST syntax of sort. The block is {$a <=> $b}. Blocks are documented in Basic BLOCKS. The operator (<=>) is described in Equality Operators. The package variables $a and $b are described through out the documentation for sort. The short answer is that this block tells sort to sort the list in numerical order rather than the default lexical order.
      Bill