testing for array or scalar in a hash.

Discussion in 'Perl Misc' started by Fred S, Jul 14, 2003.

  1. Fred S

    Fred S Guest

    Hi,
    I have a hash containing, data, everything is generated on the fly so
    my hash, has some scalars, and some arrays, in it.
    How can I test wether a value of the hash is an array and then loop
    through it ?
    i did the following loop:
    it will print the arrays but will skip the scalars,

    foreach (keys %Options){
    print $_." => \n";
    foreach my $val (@{$Options{$_}}){
    print "\t".$val;
    print "\n";
    }
    }

    Thanx for the help.


    Fred
    Fred S, Jul 14, 2003
    #1
    1. Advertising

  2. Fred S wrote:
    > I have a hash containing, data, everything is generated on the fly
    > so my hash, has some scalars, and some arrays, in it.
    > How can I test wether a value of the hash is an array and then loop
    > through it ?


    How about:

    foreach (keys %Options){
    if (ref $Options{$_} eq 'ARRAY') {
    print ...

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jul 14, 2003
    #2
    1. Advertising

  3. Fred S

    Greg Bacon Guest

    In article <>,
    Fred S <> wrote:

    : I have a hash containing, data, everything is generated on the fly so
    : my hash, has some scalars, and some arrays, in it.
    : How can I test wether a value of the hash is an array and then loop
    : through it ?
    : [snip code]

    Use Perl's ref operator to see whether you have an array reference:

    #! /usr/local/bin/perl

    use warnings;
    use strict;

    my %Options = (
    scalar => 42,
    arrayref => [ qw/ apples oranges bananas / ],
    );

    for (keys %Options) {
    print $_, " => \n",
    map "\t$_\n",
    ref $Options{$_} ? @{ $Options{$_} } : $Options{$_};
    }

    This generates the following output:

    scalar =>
    42
    arrayref =>
    apples
    oranges
    bananas

    You could pare the line lengths by iterating with the each operator.

    Hope this helps,
    Greg
    --
    The more corrupt the state, the more numerous the laws.
    -- Tacitus
    Greg Bacon, Jul 14, 2003
    #3
  4. Fred S wrote:
    >
    > I have a hash containing, data, everything is generated on the fly so
    > my hash, has some scalars, and some arrays, in it.
    > How can I test wether a value of the hash is an array and then loop
    > through it ?
    > i did the following loop:
    > it will print the arrays but will skip the scalars,
    >
    > foreach (keys %Options){
    > print $_." => \n";
    > foreach my $val (@{$Options{$_}}){
    > print "\t".$val;
    > print "\n";
    > }
    > }


    Use the ref operator.

    for ( keys %Options ) {
    print $_." => \n";
    if ( ref eq 'ARRAY' ) {
    print "\t", join( "\t", @{$Options{$_}} ), "\n";
    }
    }


    perldoc -f ref


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Jul 14, 2003
    #4
  5. Fred S <> wrote:

    > I have a hash containing, data, everything is generated on the fly
    > so my hash, has some scalars, and some arrays, in it.
    > How can I test wether a value of the hash is an array and then
    > loop through it ?


    perldoc -f ref

    Example:

    use strict;
    use warnings;

    my %h = (
    a => [1,2,3,4],
    h => {a=>1,b=>2,c=>3},
    s => 'string'
    );

    foreach my $k (sort keys %h) {
    print "$k -> ", ref $h{$k}, "\n";
    }

    Note that the output for $h{s} is blank, because $h{s} is a scalar,
    not a reference to a scalar.
    David K. Wall, Jul 14, 2003
    #5
  6. Fred S

    Bart Lateur Guest

    Fred S wrote:

    >I have a hash containing, data, everything is generated on the fly so
    >my hash, has some scalars, and some arrays, in it.
    >How can I test wether a value of the hash is an array and then loop
    >through it ?


    If *all* you have in there are array (refs) and scalars, then you can
    use ref() as a boolean test. For scalars, it will return the empty
    string (false), for array refs, "ARRAY" (true).

    --
    Bart.
    Bart Lateur, Jul 15, 2003
    #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,493
    Dmitry Roslyakov
    Jun 11, 2004
  2. rp
    Replies:
    1
    Views:
    478
    red floyd
    Nov 10, 2011
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    581
    David A. Black
    Jul 2, 2008
  4. Clint Olsen
    Replies:
    6
    Views:
    333
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  5. Mark

    Replace scalar in another scalar

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

Share This Page