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

RE: [PBML] Declaring variables

Expand Messages
  • Charles K. Clarkson
    ... They are not the same variable. $list_item[$j] is a one element in the array @list_item. $list_item is a totally different scalar variable which happens to
    Message 1 of 2 , Jun 4, 2006
    • 0 Attachment
      joshypy wrote:

      : In the following Code I declared @list_item as a local array
      : variable in the subroutine named test. If i use the array items
      : one by one in the following statement using $list_item[$j]. My
      : question is whether i should redeclare $list_item using my. i.e,
      : my $list_item.

      They are not the same variable. $list_item[$j] is a one
      element in the array @list_item. $list_item is a totally different
      scalar variable which happens to have a similar name. Declaring
      $list_item will have no effect on $list_item[$j] or on @list_item.


      : sub test
      : {
      : my @list_item=&decode_list;
      :
      : foreach(@list_item)
      : {
      : $list_item[$j]=&decimal($list_item[$j]);
      : $j++;
      : }
      : }

      As Ken pointed out, $j should be declared somewhere. Here's a
      rewrite. It reads a little easier if we use plurals for array
      names. I have changed @list_item to @list_items. In practice, I
      would use a descriptive name which identified what I was working
      on. @list_items is too generic for most applications. Finally, I
      added some white space to make things easier to read.

      sub test {
      my @list_items = &decode_list;

      my $j = 0;
      foreach ( @list_items ) {
      $list_items[$j] = &decimal( $list_items[$j] );
      $j++;
      }

      # I assume we a returning something.
      return @list_items;
      }


      It is more modern to step through array values using an alias,
      unless you absolutely have to use an index. Here, $list_item is an
      alias for each element in the array and changing its value changes
      the underlying value in the @list_items.

      sub test {
      my @list_items = &decode_list;

      foreach my $list_item ( @list_items ) {
      $list_item = decimal( $list_item );
      }

      return @list_items;
      }

      Since this is a beginner's list and &decode_list has become
      an obscure syntax, it might be better to explicitly write test()
      like this. Read perlsub on why &decode_list and decode_list(@_)
      are equivalent.

      sub test {
      my @list_items = decode_list( @_ );

      foreach my $list_item ( @list_items ) {
      $list_item = decimal( $list_item );
      }

      return @list_items;
      }



      Charles K. Clarkson
      --
      Mobile Homes Specialist
      Free Market Advocate
      Web Programmer

      254 968-8328

      Don't tread on my bandwidth. Trim your posts.

      PS

      For those of you who do not know, @_ is a special array
      holding the values passed to test(). If we call test like this,

      my @people = test( qw| John Paul Betty | );

      The Perl places 'John', 'Paul' and 'Betty' into @_ in that
      order. &decode_list is a special syntax which means
      decode_list(@_). That's just like calling this (except the
      arguments change each time we call test() again).

      my @list_items = decode_list( qw| John Paul Betty | );
    Your message has been successfully submitted and would be delivered to recipients shortly.