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

Dereferencing an referenced array element in a subroutine

Expand Messages
  • JC
    Hello All, I suppose this is a simple problem, however, I m not able to find the right incantation to make this work Code: #!/usr/bin/perl sub try; $var1 =
    Message 1 of 3 , Oct 7, 2005
    • 0 Attachment
      Hello All,

      I suppose this is a simple problem, however, I'm not
      able to find the right incantation to make this work

      Code:

      #!/usr/bin/perl
      sub try;
      $var1 = "Perl is the Best";
      @arr = qw(Theres, more, than, one, way);
      try($var1, \@arr);


      sub try{
      foreach $i (@_) {
      if(ref($i)) {
      print "Referenced Array Element 0: $i->[0] \n";
      print "The entire Array: @$i\n";
      print "Total Elements in refArray: ",@$#i, "\n";
      }
      }
      }

      The above code mostly works, my question is how would
      one identify "Total Elements" in the reference array?

      Thanks,

      JC




      __________________________________
      Yahoo! Mail - PC Magazine Editors' Choice 2005
      http://mail.yahoo.com
    • Shawn Corey
      ... Prototypes for subroutines are not recommended unless you want to do something tricky like: sub try ($ @); # The second argument *must* be an array! ...
      Message 2 of 3 , Oct 7, 2005
      • 0 Attachment
        JC wrote:
        > Hello All,
        >
        > I suppose this is a simple problem, however, I'm not
        > able to find the right incantation to make this work
        >
        > Code:
        >
        > #!/usr/bin/perl
        > sub try;

        Prototypes for subroutines are not recommended unless you want to do
        something tricky like:

        sub try ($\@); # The second argument *must* be an array!
        ...
        try( $var1, @arr ); # Note no backslash,
        # the second argument must be an array,
        # the reference is implied.

        > $var1 = "Perl is the Best";
        > @arr = qw(Theres, more, than, one, way);
        > try($var1, \@arr);
        >
        >
        > sub try{
        > foreach $i (@_) {
        > if(ref($i)) {
        > print "Referenced Array Element 0: $i->[0] \n";
        > print "The entire Array: @$i\n";
        > print "Total Elements in refArray: ",@$#i, "\n";

        print "Total Elements in refArray: ", scalar( @$i ), "\n";
        # see `perldoc -f scalar` for details.

        > }
        > }
        > }
        >
        > The above code mostly works, my question is how would
        > one identify "Total Elements" in the reference array?
        >
        > Thanks,
        >
        > JC


        --- Shawn
      • Charles K. Clarkson
        ... Always use strict and warnings. Always! Theres is a contraction for there is and is spelled there s . The qw() operator does not need commas to
        Message 3 of 3 , Oct 7, 2005
        • 0 Attachment
          JC <> wrote:

          : I suppose this is a simple problem, however, I'm not
          : able to find the right incantation to make this work
          :
          : Code:

          Always use strict and warnings. Always!

          "Theres" is a contraction for "there is" and is spelled
          "there's". The qw() operator does not need commas to separate
          words.

          Perl assumes an array should be displayed in list context
          unless we indicate that it shouldn't. The "scalar" function forces
          scalar context, which returns the length of an array.


          : #!/usr/bin/perl

          use strict;
          use warnings;

          my $var1 = 'Perl is the Best';
          my @arr = qw(There's more than one way);
          try($var1, \@arr);


          sub try {
          foreach my $i ( @_ ) {
          if ( ref $i ) {
          print "Referenced Array Element 0: $i->[0] \n";
          print "The entire Array: @$i\n";
          print 'Total Elements in referenced array: ', scalar @$i, "\n";
          }
          }
          }


          __END__

          HTH,

          Charles K. Clarkson
          --
          Mobile Homes Specialist
          254 968-8328
        Your message has been successfully submitted and would be delivered to recipients shortly.