Help with Perl operators Please!

S

scrarymary

This is part of a script
Can someone please tell me what operations are being performed
on the value t

for (my $N = 0; $N <= $Count; $N++) {
my $t = $N * $N
$d += ($t ? $t : 1)
}

Also

my @ii = unpack("V8", $chunk);

if $chunk is 32 bytes how will the bytes be unpacked into the array
@ii?

Thank you for any help
 
S

Sisyphus

scrarymary said:
This is part of a script
Can someone please tell me what operations are being performed
on the value t

for (my $N = 0; $N <= $Count; $N++) {
my $t = $N * $N

I think that should be:

my $t = $N * $N;

At the first iteration, $t is 0 (0 squared)
At the second iteration, $t is 1 (1 squared)
At the third iteration, $t is 4 (2 squared)
At the fourth iteration, $t is 9 (3 squared)
and so on ....

After $Count + 1 iterations, the loop terminates, and $t has the value
$count * $Count ($Count squared).
$d += ($t ? $t : 1)

I think that should be:

$d += ($t ? $t : 1);

That line just assigns $d + $t to $d ... unless $t is zero (or numerically
equivalent to zero) - in which case it assigns $d + 1 to $d.
}

Also

my @ii = unpack("V8", $chunk);

if $chunk is 32 bytes how will the bytes be unpacked into the array
@ii?

You can see by running:

print "@ii\n";

(The individual elements of @ii will be separated by a space.)

See 'perldoc perlop'.

Cheers,
Rob
 
I

Ian Wilson

scrarymary said:
This is part of a script

Looks a bit like homework.
http://www.catb.org/~esr/faqs/smart-questions.html#homework

Can someone please tell me what operations are being performed
on the value t

There isn't a value t, there is a scalar variable named $t.

for (my $N = 0; $N <= $Count; $N++) {
my $t = $N * $N

A scalar value is assigned to the variable $t

$d += ($t ? $t : 1)

This is equivalent to

if ($t) {
$d += $t;
} else {
$d += 1;
}

The boolean value of $t is false if it is undefined or zero.

$foo += $bar;
is equivalent to
$foo = $foo + $bar;

}

Also

my @ii = unpack("V8", $chunk);

if $chunk is 32 bytes how will the bytes be unpacked into the array
@ii?

Since I don't know, I'd either read the documentation (start at `perldoc
-f unpack`) or write a short program to find out by assigning a
carefully chosen value to $chunk and printing @ii.
 
J

J. Gleixner

scrarymary said:
This is part of a script
Can someone please tell me what operations are being performed
on the value t

That's a trick question. The answer is None.. it's a syntax error.

If it was correct.. You can answer the questions yourself by adding
a few print statements.
for (my $N = 0; $N <= $Count; $N++) {
my $t = $N * $N

print "t is set to $N * $N which is: $t\n";

print "before d = $d\n";
$d += ($t ? $t : 1)

print "after d = $d\n";
}

Also

my @ii = unpack("V8", $chunk);

if $chunk is 32 bytes how will the bytes be unpacked into the array
@ii?

Instead of asking what will happen, why not try it and see for yourself?
 
S

scrarymary

Instead of asking what will happen, why not try it and see for yourself?
Normally I would but had trouble getting things to run

Thanks for your help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top