Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Length of strings in an array

by Athanasius (Archbishop)
on Sep 20, 2020 at 16:27 UTC ( [id://11121964]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11121964]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-25 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found