# Help with Perl operators Please!

Discussion in 'Perl Misc' started by scrarymary, Jun 4, 2007.

1. ### scrarymaryGuest

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

scrarymary, Jun 4, 2007

2. ### SisyphusGuest

"scrarymary" <> wrote in message
news...
>
> 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

Sisyphus, Jun 4, 2007

3. ### Ian WilsonGuest

scrarymary wrote:
> 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.

Ian Wilson, Jun 4, 2007
4. ### J. GleixnerGuest

scrarymary wrote:
> 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?

J. Gleixner, Jun 4, 2007
5. ### scrarymaryGuest

On Tue, 5 Jun 2007 01:33:37 +1000, "Sisyphus"
<> wrote:

>Cheers,
>Rob

Thank you

scrarymary, Jun 5, 2007
6. ### scrarymaryGuest

On Mon, 04 Jun 2007 16:45:10 +0100, Ian Wilson
<> wrote:

thanks

scrarymary, Jun 5, 2007
7. ### scrarymaryGuest

On Mon, 04 Jun 2007 10:50:48 -0500, "J. Gleixner"
<> wrote:

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

Normally I would but had trouble getting things to run