jkushner jkushner - 1 year ago 74
Perl Question

Am I using the round function correctly?

Here is my Perl code

use POSIX;

my @arr = split(/\\n\\n/, $content);

my $len = length @arr;

$len = $len / 2;

my $b = round($len) - 1;

At the top of my script I have
. I once had
use Math::Round
but that didn't work.

I'm trying to use the
function but the page keeps breaking when I call it.

mob mob
Answer Source

Yes, but you are using the length function incorrectly.

length @arr does not return the size of array @arr as you might expect, it just returns the length of the size of @arr. To get the array length, you want to use scalar @arr or to "evaluate @arr in scalar context":

$len = scalar @arr;
$len = @arr;           # assignment to scalar is "scalar context"

It's also not clear if you're using split correctly.

@arr = split /\\n\\n/, $content

splits on the literal string \n\n. If you want to split $content wherever there are consecutive newlines, you just want to say

@arr = split /\n\n/, $content
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download