Professional Web Applications Themes

testing for array or scalar in a hash. - PERL Miscellaneous

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...

  1. #1

    Default testing for array or scalar in a hash.

    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 Guest

  2. #2

    Default Re: testing for array or scalar in a hash.

    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: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

  3. #3

    Default Re: testing for array or scalar in a hash.

    In article <pan.2003.07.14.19.41.48.561164reglage.ath.cx>,
    Fred S <fredboardreglage.ath.cx> 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 Guest

  4. #4

    Default Re: testing for array or scalar in a hash.

    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 Guest

  5. #5

    Default Re: testing for array or scalar in a hash.

    Fred S <fredboardreglage.ath.cx> 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 Guest

  6. #6

    Default Re: testing for array or scalar in a hash.

    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 Guest

Similar Threads

  1. Array into scalar
    By Mountain Man in forum PHP Development
    Replies: 1
    Last Post: March 6th, 11:54 PM
  2. hash of hash of array slices
    By Paul Kraus in forum PERL Beginners
    Replies: 6
    Last Post: January 26th, 05:55 PM
  3. scalar reference in hash
    By Fred. Oger in forum PERL Beginners
    Replies: 1
    Last Post: August 16th, 02:14 AM
  4. Warning: Cannot use a scalar value as an array in
    By Redcat in forum PHP Development
    Replies: 1
    Last Post: July 15th, 08:04 PM
  5. letters in a scalar to array
    By John E in forum PERL Miscellaneous
    Replies: 5
    Last Post: July 1st, 03:35 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139