Arrays Of Arrays: Is it an Array or Scalar?

Discussion in 'Perl Misc' started by Hal Vaughan, Feb 5, 2004.

  1. Hal Vaughan

    Hal Vaughan Guest

    I have a function that I'm using to perform operations on strings in an
    array. There are times where I'd like to have this function work on arrays
    of arrays. Is there a simple way to tell if the value of an element in an
    array is a scalar, or is, instead, a reference to another array?

    I know I can use a regex to see if the string matches the pattern for an
    array reference, but is there a more "elegant" and easier way to do it?

    (And, while I'm at it, is there a way to check for a hash as well? I'd
    think they'd both be done the same way.)

    Thanks!

    Hal
     
    Hal Vaughan, Feb 5, 2004
    #1
    1. Advertising

  2. Hal Vaughan

    J Krugman Guest

    In <AKtUb.185396$nt4.786330@attbi_s51> Hal Vaughan <> writes:

    >I have a function that I'm using to perform operations on strings in an
    >array. There are times where I'd like to have this function work on arrays
    >of arrays. Is there a simple way to tell if the value of an element in an
    >array is a scalar, or is, instead, a reference to another array?


    >I know I can use a regex to see if the string matches the pattern for an
    >array reference, but is there a more "elegant" and easier way to do it?


    >(And, while I'm at it, is there a way to check for a hash as well? I'd
    >think they'd both be done the same way.)




    perldoc -f ref

    for my $elem (@array) {
    if (my $r = ref $elem) {
    if ($r eq 'ARRAY') {
    # process arrayref
    }
    elsif ($r eq 'HASH') {
    # process hashref
    }
    else {
    # deal with non-array, non-hash ref
    }
    }
    }
     
    J Krugman, Feb 5, 2004
    #2
    1. Advertising

  3. Hal Vaughan

    Graham Wood Guest

    Hal Vaughan wrote:

    > I have a function that I'm using to perform operations on strings in an
    > array. There are times where I'd like to have this function work on arrays
    > of arrays. Is there a simple way to tell if the value of an element in an
    > array is a scalar, or is, instead, a reference to another array?
    >
    > I know I can use a regex to see if the string matches the pattern for an
    > array reference, but is there a more "elegant" and easier way to do it?
    >
    > (And, while I'm at it, is there a way to check for a hash as well? I'd
    > think they'd both be done the same way.)
    >
    > Thanks!
    >
    > Hal


    You could use the ref function to find out what is in your array
    elements. If it isn't a reference to a scalar, an array, a hash, code,
    another reference or a glob or lvalue, then you can safely say it's a
    scalar. I think. I'm sure someone will correct me if I lie.

    perldoc -f ref
    ref Returns a true value if EXPR is a reference, false otherwise. If
    EXPR is not specified, "$_" will be used. The value returned
    depends on the type of thing the reference is a reference to.
    Builtin types include:

    SCALAR
    ARRAY
    HASH
    CODE
    REF
    GLOB
    LVALUE

    Graham
     
    Graham Wood, Feb 5, 2004
    #3
  4. Hal Vaughan

    Hal Vaughan Guest

    Hal Vaughan wrote:

    > I have a function that I'm using to perform operations on strings in an
    > array. There are times where I'd like to have this function work on
    > arrays
    > of arrays. Is there a simple way to tell if the value of an element in an
    > array is a scalar, or is, instead, a reference to another array?
    >
    > I know I can use a regex to see if the string matches the pattern for an
    > array reference, but is there a more "elegant" and easier way to do it?
    >
    > (And, while I'm at it, is there a way to check for a hash as well? I'd
    > think they'd both be done the same way.)
    >
    > Thanks!
    >
    > Hal


    Thanks to those who responded and said to use the ref() function. I was
    thinking more along the lines of type() or typeof(), and couldn't find
    anything like that. Once someone pointed me in the right direction (with
    ref() ), it was easy to find more info.

    The problem with being self-taught is that there are so many holes like this
    where you know there should be something, but you have no idea what it's
    called, so it can be hard to find. Once you get a name or term to use,
    it's easy.

    Thanks.

    Hal
     
    Hal Vaughan, Feb 5, 2004
    #4
  5. Hal Vaughan <> wrote in message news:<AKtUb.185396$nt4.786330@attbi_s51>...
    > Is there a simple way to tell if the value of an element in an
    > array is a scalar, or is, instead, a reference to another array?


    You're confusing your terms a bit. Let's start from the basics:

    Arrays are collections of scalars (not anything else... ever).

    Scalars can be simple values, references, or other (less common)
    things I won't go into here.

    So, to re-phase what I think you meant:

    "Is there a simple way to tell if an element in an array is a
    reference?"

    And the answer there would be "ref". You can use "perldoc -f ref" or
    "perldoc perlrref" for far, far more information on the topic, but a
    reference to an array would return "ARRAY" when you use ref on it.
    Thus, you might say:

    if (ref($myarray[$i])) {
    if (ref($myarray[$i]) eq 'ARRAY') {
    # It's a reference to a sub-array, do the right thing....
    } else {
    warn "I don't know how to cope with refs to
    ".ref($myarray[$i])." in my array":
    }
    } else {
    # It's a simple scalar value, do the right thing.....
    }

    Good luck!
     
    Aaron Sherman, Feb 5, 2004
    #5
  6. Hal Vaughan

    Tore Aursand Guest

    On Thu, 05 Feb 2004 15:58:24 +0000, Hal Vaughan wrote:
    > I have a function that I'm using to perform operations on strings in an
    > array. There are times where I'd like to have this function work on arrays
    > of arrays. Is there a simple way to tell if the value of an element in an
    > array is a scalar, or is, instead, a reference to another array?


    Yes, by using the ref() function;

    perldoc -f ref

    I feel that I personally tend to write functions/methods that no matter
    what turns the argument data into an array reference (if that's applicable);

    sub foo {
    my $input = shift;
    $input = ( ref($input) eq 'ARRAY' ) ? $input : [ $input ];

    foreach ( @$input ) {
    # ...
    }
    }

    That way I never (...) need to know what the function/method wants.


    --
    Tore Aursand <>
    "I am become Death, shatterer of worlds." -- J. Robert Oppenheimer,
    upon witnessing the explosion of the first atomic bomb.
     
    Tore Aursand, Feb 6, 2004
    #6
    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. Dmitri Zakharov
    Replies:
    2
    Views:
    6,522
    Dmitry Roslyakov
    Jun 11, 2004
  2. John E

    letters in a scalar to array

    John E, Jul 1, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    95
    Carlton Brown
    Jul 1, 2003
  3. Fred S
    Replies:
    5
    Views:
    90
    Bart Lateur
    Jul 15, 2003
  4. Clint Olsen
    Replies:
    6
    Views:
    362
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  5. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    164
    Arndt Jonasson
    Jan 27, 2005
Loading...

Share This Page