# Use of uninitialized value

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
This script is presenting 'Use of uninitialized value' beyond the wrong output values. What can this be?
Thank you.

#! / usr / bin / perl -w
use strict;

#funcoes1.pl

my (\$ n1, n2 \$, \$ n3) =ARGV;

print "The media and:" Average (\$ n1, n2 \$, \$ n3), "\ n";

My (num);
num = (n1 \$, \$ n2, n3 \$);
print "The media and:" media2 (num), "\ n";

my (\$ numN);
\$ numN = 6;
num = (n1 \$, \$ n2, n3 \$, 2, 3, 4);
print "The media and:" Median (\$ numN,num), "\ n";

average sub {
my (\$ a, \$ b, \$ c) =_;
my (\$ result);
\$ result = (\$ a + \$ b + \$ c) / 3;
return \$ result;
}

media2 sub {
my param =_;
my (\$ result);
\$ result = (\$ param [0] + \$ param [1] + \$ param [2]) / 3;
return \$ result;
}

Median sub {
my (\$ n, param) =_;
my (\$ result);
for (my \$ i = 0; \$ i <\$ N; \$ i ++) {
\$ result = + \$ param [\$ i];
}
return \$ result / \$ n;
}

Output:

Use of uninitialized value \$b in addition (+) at funcoes1.pl line 26.
Use of uninitialized value \$a in addition (+) at funcoes1.pl line 26.
Use of uninitialized value \$c in addition (+) at funcoes1.pl line 26.
A media e: 0
Use of uninitialized value \$param[1] in addition (+) at funcoes1.pl line 34.
Use of uninitialized value \$param[0] in addition (+) at funcoes1.pl line 34.
Use of uninitialized value \$param[2] in addition (+) at funcoes1.pl line 34.
A media e: 0
Use of uninitialized value \$param[0] in addition (+) at funcoes1.pl line 42.
Use of uninitialized value \$param[1] in addition (+) at funcoes1.pl line 42.
Use of uninitialized value \$param[2] in addition (+) at funcoes1.pl line 42.
A media e: 1.5

## Re: Use of uninitialized value

On 01/10/15 17:17, Cleuson Oliveira wrote:

... etc

What you posted isn't the code that you ran, is it?

perl -c wrong.pl
Bareword found where operator expected at wrong.pl line 8, near ""The
media and:" Average"
(Missing operator before Average?)
Bareword found where operator expected at wrong.pl line 12, near ""The
media and:" media2"
(Missing operator before media2?)
Bareword found where operator expected at wrong.pl line 17, near ""The
media and:" Median"
(Missing operator before Median?)
Can't use global \$, in "my" at wrong.pl line 6, near "n2 \$, "
Global symbol "\$n3" requires explicit package name at wrong.pl line 6.
syntax error at wrong.pl line 8, near ""The media and:" Average "
Global symbol "\$n3" requires explicit package name at wrong.pl line 8.
Global symbol "\$n2" requires explicit package name at wrong.pl line 11.
syntax error at wrong.pl line 11, near "\$);"
syntax error at wrong.pl line 12, near ""The media and:" media2 "
Global symbol "\$n2" requires explicit package name at wrong.pl line 16.
syntax error at wrong.pl line 17, near ""The media and:" Median "
syntax error at wrong.pl line 24, near "}"
wrong.pl has too many errors.

(Or has your news posting client done something strange?  The code in my
"wrong.pl" is copied directly from your post.)

--

Henry Law            Manchester, England

## Re: Use of uninitialized value

This is a runtime error

This line produces compile time errors.

Therefore the code you posted cannot possibly be the same code that
produced the runtime error.

jue

## Re: Use of uninitialized value

On Friday, 2 October 2015 00:38:28 UTC+5:30, Jürgen Exner  wrote:

First time I saw this:

>>     #! / usr / bin / perl -w

What to talk of compilation, this file would not even start.

Is the OP drinking & posting?

## Re: Use of uninitialized value

Em quinta-feira, 1 de outubro de 2015 13:17:47 UTC-3, Cleuson Oliveira  esc
reveu:

utput values. What can this be?

34.
34.
34.
42.
42.
42.

Sorry,
Follows the correction

#!/usr/bin/perl -w
use strict;

# O arranjo @ARGV é o vetor que recebe os parâmetros passados na linha
de comando
my (\$n1, \$n2, \$n3) = @ARGV;

# Passagem de variáveis escalares como parâmetro
print "A media e: ",media(\$n1, \$n2, \$n3),"\n";

# Passagem de um arranjo como parâmetro
my (@num);
@num = (\$n1, \$n2, \$n3);
print "A media e: " ,media2(@num),"\n";

# Passagem de variável escalar mais um arranjo como parâmetro
my (\$numN);
\$numN = 6;
@num = (\$n1, \$n2, \$n3, 2, 3, 4);
print "A media e: ",mediaN(\$numN, @num),"\n";

# Função que calcula a media entre 2 numeros recebidos como parâmetro
s como variáveis escalares

sub media {
my (\$a, \$b, \$c) = @_;
my (\$result);
\$result = (\$a+\$b+\$c) / 3;
return \$result;
}

# Função que calcula a media entre 2 números recebidos como parâmet
ros como arranjo
sub media2 {
my @param = @_;
my (\$result);
\$result = (\$param[0] + \$param[1] + \$param[2]) / 3;
return \$result;
}

# Função que calcula a media entre n números
sub mediaN {
my (\$n, @param) = @_;
my (\$result);
for (my \$i=0;\$i<\$n;\$i++) {
\$result += \$param[\$i];
}
return \$result / \$n;
}

Output:
Use of uninitialized value \$b in addition (+) at funcoes1.pl line 26.
Use of uninitialized value \$a in addition (+) at funcoes1.pl line 26.
Use of uninitialized value \$c in addition (+) at funcoes1.pl line 26.
A media e: 0
Use of uninitialized value \$param[1] in addition (+) at funcoes1.pl line 34
.
Use of uninitialized value \$param[0] in addition (+) at funcoes1.pl line 34
.
Use of uninitialized value \$param[2] in addition (+) at funcoes1.pl line 34
.
A media e: 0
Use of uninitialized value \$param[0] in addition (+) at funcoes1.pl line 42
.
Use of uninitialized value \$param[1] in addition (+) at funcoes1.pl line 42
.
Use of uninitialized value \$param[2] in addition (+) at funcoes1.pl line 42
.
A media e: 1.5

## Re: Use of uninitialized value

On Friday, 2 October 2015 06:35:49 UTC+5:30, Cleuson Oliveira  wrote:
[snip]

Since you have 3 scalar variables on the left, so you must ensure
@ARGV has at least 3 elements, lest some will be left undefined.
For ex., @ARGV has 2 elements, then \$n1/\$n2 ok but \$n3 left undefined.

Your error/warning messages stem from the fact that the sub
media was not provided with defined values. You were able to see
these is due to your -w option used.

Since you are using the "-w" switch, means you care about
the values that go in to your code. And that is good.
So check for the @ARGV to ensure that it is able to provide
for the three scalars, something like,

die "insufficient args given..." if @ARGV < 3

e)

die "div by zero...." if \$n == 0;
die "nothing to comput..." @param == 0;
die "bad arguments..." if \$n != @param

You may want to also add assertion to all your arguments are numeric so
that the results are not nonsensical.

## Re: Use of uninitialized value

On 2/10/2015 04:05, Cleuson Oliveira wrote:

#!/usr/bin/perl -w
use strict;use warnings; use feature 'say';

my @num;
\$num[0]=exists \$ARGV[0]?\$ARGV[0]:die "1st arg is missing\n";
\$num[1]=exists \$ARGV[1]?\$ARGV[1]:die "2ng arg is missing\n";
\$num[2]=exists \$ARGV[2]?\$ARGV[2]:die "3rd arg is missing\n";

say 'A media e: ',media(@num);
say 'A media e: ',media2(@num);
say 'A media e: ',mediaN(6, @num,2, 3, 4);

sub media  { (\$_[0]+\$_[1]+\$_[2]) / 3 }
sub media2 { (\$_[0]+\$_[1]+\$_[2]) / 3 }
sub mediaN {
my \$n = shift;
my \$result;
\$result += \$_ for @_;
\$result / \$n
}

## Re: Use of uninitialized value

This is ugly and wrong.

If this is missing then *all* arguments are missing.

If this is missing then two arguments are missing, how can you be sure
the argument in \$ARGV[0] is the argument that should be there, it could
just as likely be the argument that should be in \$ARGV[2].

"An argument is missing", you don't know which one.

Justin.

--
Justin C, by the sea.

## Re: Use of uninitialized value

On 2/10/2015 11:41, Justin C wrote:

this is very beautiful and absolute correct , sorry.

so ?

as you may understand if the 0 is missing it will be reported from the
previous line statement

I know exactly the 2 is missing because of the previous two statements ,
sorry if you have problem , but thats the way it is

G.M

## Re: Use of uninitialized value

[snip]

How can \$ARGV[0] be missing and yet \$ARGV[1] exist? If there are two
elements in an array they are indexed 0 and 1, passed by @ARGV it is
not possible to have 1 and 2, and zero not exist.

Justin.

--
Justin C, by the sea.

## Re: Use of uninitialized value

[...]

Assuming 3 arguments are required but only 2 were provided, it's not
possible to tell if the 1st, the 2nd or the 3rd was left out. Eg,
assuming my 'input values' were 1, 2 and 3, the script could accidentally
have been invoked as

script 1 2
script 2 3
script 1 3

--------
@ARGV == 3 or die("need 3 arguments");

say 'A media e: ',media(@ARGV);
say 'A media e: ',media2(@ARGV);
say 'A media e: ',mediaN(6, @ARGV,2, 3, 4);

## Re: Use of uninitialized value

On 2/10/2015 14:39, Rainer Weikusat wrote: