Help with Perl operators Please!

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

  1. scrarymary

    scrarymary Guest

    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
    #1
    1. Advertising

  2. scrarymary

    Sisyphus Guest

    "scrarymary" <> wrote in message
    news:eek:...
    >
    > 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
    #2
    1. Advertising

  3. scrarymary

    Ian Wilson Guest

    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
    #3
  4. scrarymary

    J. Gleixner Guest

    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
    #4
  5. scrarymary

    scrarymary Guest

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

    >Cheers,
    >Rob


    Thank you
    scrarymary, Jun 5, 2007
    #5
  6. scrarymary

    scrarymary Guest

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

    thanks
    scrarymary, Jun 5, 2007
    #6
  7. scrarymary

    scrarymary Guest

    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

    Thanks for your help
    scrarymary, Jun 5, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,767
  2. Replies:
    4
    Views:
    491
    Chris Uppal
    May 5, 2005
  3. KK
    Replies:
    2
    Views:
    516
    Big Brian
    Oct 14, 2003
  4. MuZZy
    Replies:
    7
    Views:
    1,717
    Mike Hewson
    Jan 7, 2005
  5. I H H
    Replies:
    6
    Views:
    122
    Peter Wyzl
    Nov 12, 2004
Loading...

Share This Page