Professional Web Applications Themes

what is this data structure? - PERL Beginners

Hi, I don't know how to work with this data structure: my array = ((a => 'appple'), (b => 'tree'), (c => 'chair')); when I do: print array; I get no output. when I do: use Data::Dumper; print Dumper(array); I got the contents of it. Thanks, Jack...

  1. #1

    Default what is this data structure?

    Hi,

    I don't know how to work with this data structure:

    my array = ((a => 'appple'),
    (b => 'tree'),
    (c => 'chair'));

    when I do:

    print array;

    I get no output.

    when I do:

    use Data::Dumper;
    print Dumper(array);

    I got the contents of it.

    Thanks,

    Jack


    Jack Chen Guest

  2. #2

    Default Re: what is this data structure?

    On 1/16/2004 11:03 AM, Jack Chen wrote:
    > Hi,
    >
    > I don't know how to work with this data structure:
    >
    > my array = ((a => 'appple'),
    > (b => 'tree'),
    > (c => 'chair'));
    >
    > when I do:
    >
    > print array;
    >
    > I get no output.
    >
    > when I do:
    >
    > use Data::Dumper;
    > print Dumper(array);
    >
    > I got the contents of it.
    Despite all of the punctuation, it is nothing more than an array. The
    following is equivelant:

    my array = qw( a apple b tree c chair );

    I find it more helpfull when using Data::Dumper to print a reference to
    the variable I want to examine. I.e.

    print Dumper(\array);

    To me it makes the structure easier to interpret. I don't understand
    though why you saw nothing for 'print array', are you sure you ran the
    code as above. I get:

    print array;
    => aappplebtreecchair

    print "array";
    => a appple b tree c chair

    Regards,
    Randy.




    Randy W. Sims Guest

  3. #3

    Default Re: what is this data structure?

    Hi Randy,

    Thanks for reply!

    It is strage. When I tried the script on another machine, it works the way
    you described. Anything wrong about my perl or my OS (Linux 8)?

    Jack

    On Fri, 16 Jan 2004, Randy W. Sims wrote:
    > On 1/16/2004 11:03 AM, Jack Chen wrote:
    >
    > > Hi,
    > >
    > > I don't know how to work with this data structure:
    > >
    > > my array = ((a => 'appple'),
    > > (b => 'tree'),
    > > (c => 'chair'));
    > >
    > > when I do:
    > >
    > > print array;
    > >
    > > I get no output.
    > >
    > > when I do:
    > >
    > > use Data::Dumper;
    > > print Dumper(array);
    > >
    > > I got the contents of it.
    >
    > Despite all of the punctuation, it is nothing more than an array. The
    > following is equivelant:
    >
    > my array = qw( a apple b tree c chair );
    >
    > I find it more helpfull when using Data::Dumper to print a reference to
    > the variable I want to examine. I.e.
    >
    > print Dumper(\array);
    >
    > To me it makes the structure easier to interpret. I don't understand
    > though why you saw nothing for 'print array', are you sure you ran the
    > code as above. I get:
    >
    > print array;
    > => aappplebtreecchair
    >
    > print "array";
    > => a appple b tree c chair
    >
    > Regards,
    > Randy.
    >
    >
    >
    >
    >
    Jack Chen Guest

  4. #4

    Default RE: what is this data structure?

    Jack Chen <chenncshl.edu> wrote:
    :
    : I don't know how to work with this data structure:
    :
    : my array = ((a => 'appple'),
    : (b => 'tree'),
    : (c => 'chair'));
    :
    : when I do:
    :
    : print array;
    :
    : I get no output.

    It printed okay for me. Perhaps something else is
    wrong. Show us everything.

    aappplebtreecchair


    HTH,

    Charles K. Clarkson
    --
    Head Bottle Washer,
    Clarkson Energy Homes, Inc.
    Mobile Home Specialists
    254 968-8328

    Charles K. Clarkson Guest

  5. #5

    Default Re: what is this data structure?

    On Fri, 2004-01-16 at 11:03, Jack Chen wrote:
    > Hi,
    >
    > I don't know how to work with this data structure:
    >
    > my array = ((a => 'appple'),
    > (b => 'tree'),
    > (c => 'chair'));
    That "array" is an array of hashes.

    -Dan

    Dan Anderson Guest

  6. #6

    Default Re: what is this data structure?

    On Jan 17, 2004, at 2:02 PM, Dan Anderson wrote:
    > On Fri, 2004-01-16 at 11:03, Jack Chen wrote:
    >> Hi,
    >>
    >> I don't know how to work with this data structure:
    >>
    >> my array = ((a => 'appple'),
    >> (b => 'tree'),
    >> (c => 'chair'));
    >
    > That "array" is an array of hashes.
    It was probably meant to be, but it is not as written. Look again.

    James

    James Edward Gray II Guest

  7. #7

    Default Re: what is this data structure?

    On Sat, 2004-01-17 at 15:12, James Edward Gray II wrote:
    > On Jan 17, 2004, at 2:02 PM, Dan Anderson wrote:
    >
    > > On Fri, 2004-01-16 at 11:03, Jack Chen wrote:
    > >> Hi,
    > >>
    > >> I don't know how to work with this data structure:
    > >>
    > >> my array = ((a => 'appple'),
    > >> (b => 'tree'),
    > >> (c => 'chair'));
    > >
    > > That "array" is an array of hashes.
    >
    > It was probably meant to be, but it is not as written. Look again.
    Ahhh, you're right. Correct me if I'm wrong, but the arrays expand out,
    so although (a -> 'apple') is a hash and (b => 'tree') is a hash, it
    actually contains the hash:

    (
    a => 'apple',
    b => 'tree',
    c => 'chair',
    )

    Correct?

    -Dan

    Dan Anderson Guest

  8. #8

    Default Re: what is this data structure?

    On 1/17/2004 3:16 PM, Dan Anderson wrote:
    > On Sat, 2004-01-17 at 15:12, James Edward Gray II wrote:
    >
    >>On Jan 17, 2004, at 2:02 PM, Dan Anderson wrote:
    >>
    >>
    >>>On Fri, 2004-01-16 at 11:03, Jack Chen wrote:
    >>>
    >>>>Hi,
    >>>>
    >>>>I don't know how to work with this data structure:
    >>>>
    >>>>my array = ((a => 'appple'),
    >>>> (b => 'tree'),
    >>>> (c => 'chair'));
    >>>
    >>>That "array" is an array of hashes.
    >>
    >>It was probably meant to be, but it is not as written. Look again.
    >
    >
    > Ahhh, you're right. Correct me if I'm wrong, but the arrays expand out,
    > so although (a -> 'apple') is a hash and (b => 'tree') is a hash, it
    > actually contains the hash:
    >
    > (
    > a => 'apple',
    > b => 'tree',
    > c => 'chair',
    > )
    >
    > Correct?
    Yes. One thing that may help is to remember that => is just a fancy
    comma and the parentheses are only for grouping-They don't create any
    structure or change the type of the thing they're grouping.


    Randy W. Sims Guest

  9. #9

    Default Re: what is this data structure?

    On Jan 17, 2004, at 2:16 PM, Dan Anderson wrote:
    > On Sat, 2004-01-17 at 15:12, James Edward Gray II wrote:
    >> On Jan 17, 2004, at 2:02 PM, Dan Anderson wrote:
    >>
    >>> On Fri, 2004-01-16 at 11:03, Jack Chen wrote:
    >>>> Hi,
    >>>>
    >>>> I don't know how to work with this data structure:
    >>>>
    >>>> my array = ((a => 'appple'),
    >>>> (b => 'tree'),
    >>>> (c => 'chair'));
    >>>
    >>> That "array" is an array of hashes.
    >>
    >> It was probably meant to be, but it is not as written. Look again.
    >
    > Ahhh, you're right. Correct me if I'm wrong, but the arrays expand
    > out,
    > so although (a -> 'apple') is a hash and (b => 'tree') is a hash, it
    > actually contains the hash:
    You're terminology is a little confusing, but I think you have the
    right idea.

    ('a', 'apple') # this is a list, not an array or a hash
    (a => 'apple') # this is the exact same list

    (a => 'apple', b => 'tree') # this is just a bigger list
    # and because Perl flattens all nested lists...
    ((a => 'apple'), (b => 'tree')) # this is the same as the above, just
    one big list

    my array = ((a => 'apple'), (b => 'tree')); # makes a four element
    array
    my %hash = ((a => 'apple'), (b => 'tree')); # makes a two key hash

    Hope that clears things up.

    James
    > (
    > a => 'apple',
    > b => 'tree',
    > c => 'chair',
    > )
    >
    > Correct?
    >
    > -Dan
    >
    >
    James Edward Gray II Guest

  10. #10

    Default Re: what is this data structure?

    > You're terminology is a little confusing, but I think you have the
    > right idea.
    For the benefit of the original poster:

    The => symbol is usually used to signify a hash and automatically quote
    the key to the left of it. I.e.:

    my %hash1 = (
    foo => 'bar',
    bar => 'baz',
    baz => 'foo',
    );

    However a hash is just an even valued list, so %hash1 is the same as
    %hash2:

    my %hash2 = ('foo', 'bar', 'bar', 'baz', 'baz', 'foo');

    However, a lot of times you'll see a hash in a hash reference, i.e.:

    my $hashref = { foo => 'bar', bar => 'baz', baz => 'foo', };

    So that instead of accessing elements by $hash1{foo} you'd access them
    by $hash1->{foo}.

    I think I've successfully improved, right?

    -Dan

    Dan Anderson Guest

  11. #11

    Default Re: what is this data structure?

    Dan Anderson wrote:
    > However a hash is just an even valued list, so %hash1 is the same as
    > %hash2:
    > So that instead of accessing elements by $hash1{foo} you'd access them
    > by $hash1->{foo}.
    >
    > I think I've successfully improved, right?
    Yes. You're closer, but still a little off.:
    > However a hash is just an even valued list, so %hash1 is the same as
    > %hash2:
    A hash is a heckuva lot more than an even valued list. Hashes take lists
    with even number of values as static initializers. They also serialize
    themselves as similar lists for transfer through interfaces. When intact,
    though, they use storage and retrieval algorithms that have nothing to do
    with sinple array structures. It is the transfer process that squeezes the
    magic out of them.

    This is why we use references. By leaving the hash in place, and passing
    a variable by which the intact structure can be accessed, we harness its
    full power, which is truly immense.

    Joseph

    R. Joseph Newton Guest

Similar Threads

  1. cf structure and unicode data
    By shoalwater in forum Coldfusion - Advanced Techniques
    Replies: 3
    Last Post: May 3rd, 02:17 PM
  2. data structure from XML::LibXML
    By Dave Roe in forum PERL Modules
    Replies: 2
    Last Post: October 8th, 02:26 AM
  3. Structure Data Types
    By Donald Nova in forum ASP.NET Web Services
    Replies: 0
    Last Post: March 2nd, 01:20 PM
  4. What would be the best data structure to keep these values
    By Steve Hemond in forum PERL Beginners
    Replies: 7
    Last Post: December 17th, 03:13 AM
  5. data structure help
    By James E Keenan in forum PERL Miscellaneous
    Replies: 0
    Last Post: August 9th, 06:47 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