Trouble understanding data structure

Discussion in 'Perl Misc' started by Page, Oct 21, 2004.

  1. Page

    Page Guest

    I have some XML that I read in and print out using Data::Dumper which
    has helped me tremendously, but I'm struggling with some parts of it.

    Here is the sample Dumper output:
    $VAR1 = {
    'detail' => [
    {
    'table' => [
    {
    'tr' => [
    {
    'td' => [
    {
    'content' => '2316692',
    'form' => '$',
    'm' => '1',
    'type' => 'red'
    },
    ' ',
    ' ',
    ' '
    ]
    }
    ],
    'tt' => [
    'Daily'
    ],
    etc...

    The problem is when I try to loop through the table. Sometimes the
    child is a tt and sometimes the child is a tr and in other cases it is
    a th.

    How do I check to see which one it is?
    for (@{$struct->{detail}->[0]->{table}}) {
    # I want to now loop through all the children but do different
    stuff dependinging on whether the child is a tr, tt, or th.
    }
     
    Page, Oct 21, 2004
    #1
    1. Advertising

  2. Page wrote:

    > I have some XML that I read in and print out using Data::Dumper which
    > has helped me tremendously, but I'm struggling with some parts of it.
    >
    > Here is the sample Dumper output:
    > $VAR1 = {
    > 'detail' => [
    > {
    > 'table' => [
    > {
    > 'tr' => [
    > {
    > 'td' => [
    > {
    > 'content' => '2316692',
    > 'form' => '$',
    > 'm' => '1',
    > 'type' => 'red'
    > },
    > ' ',
    > ' ',
    > ' '
    > ]
    > }
    > ],
    > 'tt' => [
    > 'Daily'
    > ],
    > etc...


    I don't think you've cut-and-pasted that literally.

    >
    > The problem is when I try to loop through the table. Sometimes the
    > child is a tt and sometimes the child is a tr and in other cases it is
    > a th.
    >
    > How do I check to see which one it is?
    > for (@{$struct->{detail}->[0]->{table}}) {
    > # I want to now loop through all the children but do different
    > stuff dependinging on whether the child is a tr, tt, or th.
    > }


    I think you have an array of single element hashes.

    You can find the key value of a single element hash using:

    my ($tag) = keys %$_; # Note the list context
     
    Brian McCauley, Oct 23, 2004
    #2
    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. Ric Pullen

    Trouble understanding StateServer!!

    Ric Pullen, Jul 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    382
    Ric Pullen
    Jul 23, 2004
  2. Rob
    Replies:
    1
    Views:
    454
    Victor Bazarov
    Mar 7, 2004
  3. Jakle

    trouble understanding None

    Jakle, Nov 12, 2003, in forum: Python
    Replies:
    9
    Views:
    351
    Alex Martelli
    Nov 13, 2003
  4. drhowarddrfine

    Trouble accessing data with pointer to structure

    drhowarddrfine, Dec 9, 2007, in forum: C Programming
    Replies:
    2
    Views:
    311
    drhowarddrfine
    Dec 9, 2007
  5. A
    Replies:
    27
    Views:
    1,658
    Jorgen Grahn
    Apr 17, 2011
Loading...

Share This Page