Loading ...
Sorry, an error occurred while loading the content.

Re: Can zero and undef be distinguished?

Expand Messages
  • a_z0_9_blah
    ... elements. ... Maybe something like this: for (@array[0..3]) { if (defined) { ...do whatever } else { ...$_ is undefined } } Chris
    Message 1 of 5 , Jul 9, 2007
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, aditi gupta <aditi9783@...>
      wrote:
      >
      >
      > Hello All:
      >
      > I have an array, @array = (0, undef, 2, undef);
      > And I want different operations on numerical and undef array
      elements.
      >
      > Since numerically undef behaves as zero, but undef are strings of
      length zero, I did the following to distinguish between the two:
      >
      > $len = length($array[$i]) # where $i-> 0 - 3
      > if ($len)
      > {
      > # the array element is numerical
      > }
      > else
      > {
      > # the array element is undef
      > }
      >
      > This works. However, this also returns the warning (though
      expectedly):
      > "Use of initialized value in length at line..."
      >
      > Is there a better way to do so?
      >
      > Thank You!
      >
      > Aditi

      Maybe something like this:

      for (@array[0..3]) {
      if (defined) {
      ...do whatever
      }
      else {
      ...$_ is undefined
      }
      }

      Chris
    • Jenda Krynicky
      To: perl-beginner@yahoogroups.com From: aditi gupta Date sent: Mon, 9 Jul 2007 21:34:17 +0100 (BST)
      Message 2 of 5 , Jul 9, 2007
      • 0 Attachment
        To: perl-beginner@yahoogroups.com
        From: aditi gupta <aditi9783@...>
        Date sent: Mon, 9 Jul 2007 21:34:17 +0100 (BST)
        Subject: [PBML] Can zero and undef be distinguished?
        Send reply to: perl-beginner@yahoogroups.com

        >
        > Hello All:
        >
        > I have an array, @array = (0, undef, 2, undef);
        > And I want different operations on numerical and undef array elements.

        perldoc -f defined

        Jenda
        ===== Jenda@... === http://Jenda.Krynicky.cz =====
        When it comes to wine, women and song, wizards are allowed
        to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery
      • aditi gupta
        Thank You Chris and Jenda!! :) Jenda Krynicky wrote: To: perl-beginner@yahoogroups.com From:
        Message 3 of 5 , Jul 9, 2007
        • 0 Attachment
          Thank You Chris and Jenda!! :)

          Jenda Krynicky <Jenda@...> wrote: To: perl-beginner@yahoogroups.com
          From: aditi gupta <aditi9783@...>
          Date sent: Mon, 9 Jul 2007 21:34:17 +0100 (BST)
          Subject: [PBML] Can zero and undef be distinguished?
          Send reply to: perl-beginner@yahoogroups.com

          >
          > Hello All:
          >
          > I have an array, @array = (0, undef, 2, undef);
          > And I want different operations on numerical and undef array elements.

          perldoc -f defined

          Jenda
          ===== Jenda@... === http://Jenda.Krynicky.cz =====
          When it comes to wine, women and song, wizards are allowed
          to get drunk and croon as much as they like.
          -- Terry Pratchett in Sourcery






          ---------------------------------
          Here’s a new way to find what you're looking for - Yahoo! Answers

          [Non-text portions of this message have been removed]
        Your message has been successfully submitted and would be delivered to recipients shortly.