Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help with Perl operators Please!

Reply
Thread Tools

Help with Perl operators Please!

 
 
scrarymary
Guest
Posts: n/a
 
      06-04-2007

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


 
Reply With Quote
 
 
 
 
Sisyphus
Guest
Posts: n/a
 
      06-04-2007

"scrarymary" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
>
> 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

 
Reply With Quote
 
 
 
 
Ian Wilson
Guest
Posts: n/a
 
      06-04-2007
scrarymary wrote:
> This is part of a script


Looks a bit like homework.
http://www.catb.org/~esr/faqs/smart-....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.

 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      06-04-2007
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?
 
Reply With Quote
 
scrarymary
Guest
Posts: n/a
 
      06-05-2007
On Tue, 5 Jun 2007 01:33:37 +1000, "Sisyphus"
<(E-Mail Removed)> wrote:

>Cheers,
>Rob


Thank you

 
Reply With Quote
 
scrarymary
Guest
Posts: n/a
 
      06-05-2007
On Mon, 04 Jun 2007 16:45:10 +0100, Ian Wilson
<(E-Mail Removed)> wrote:

thanks
 
Reply With Quote
 
scrarymary
Guest
Posts: n/a
 
      06-05-2007
On Mon, 04 Jun 2007 10:50:48 -0500, "J. Gleixner"
<(E-Mail Removed)> 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

Thanks for your help

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Problems with Arithmetic Operators in a Perl hash coolchick Perl Misc 5 11-05-2007 01:01 PM
human nature of perl (new operators etc) I H H Perl Misc 6 11-12-2004 12:16 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments