import array like structure using perl

Discussion in 'Perl Misc' started by Rudra Banerjee, Dec 26, 2012.

  1. Dear friends,
    I am trying to write a gcstar export for Latex, and I am a perl-illiterate.
    From other gcstar scripts, I am following the pattern.
    The gcstar structure is like:

    <item
    id="1"
    title="A Beautiful Mind"
    date="2001"
    time="130 min."
    director="Ron Howard"
    image="Movies_pictures/A_Beautiful_Mind_0.jpg"
    backpic=""
    original="A Beautiful Mind"
    webPage="http://www.filmaffinity.com/en/film326587.html##Film affinity (EN)"
    >

    <country>
    <line>
    <col>United States</col>
    </line>
    </country>
    <genre>
    <line>
    <col>Drama</col>
    </line>
    </genre>
    </item>

    Now, the other fields with key value structure is done, but I don't understand how to get things like
    <country>
    <line>
    <col>United States</col>
    </line>
    </country>

    My complete code for the export may be quite too large, but for a snippet, what I am trying to do is as:
    sub getItem {
    my ($self, $item, $number) = @_;
    my $result;
    return '' if ($self->{options}->{one} &&
    $item->{number} ne $self->{options}->{disc});
    $result .= '\item {\bf ' . $self->transformValue ($item->{title},"title") . "}";
    # $result .= " ($item->{date})" if $item->{date};
    # one line for russian cartoons
    if ($self->transformValue ($item->{genre}, 'genre') =~
    m/Мультфильм/) {
    $result .= ' м/ф';
    } elsif ($item->{genre} || $item->{director} ||
    $item->{audio} || $item->{time}) {
    $result .= "\\\\\n\\begin{tabular}{l|ll}\n";
    $result .= '\multirow{5}{*}{\includegraphics[height=1in]{'. $self->transformValue ($item->{image}, 'image') . '}}'
    if $item->{image};
    $result .='\\ & ' . $self->getLocal('date') . ': & ' .
    $self->transformValue ($item->{date}, 'date') . '\\'
    if $item->{date};

    $result .='\\ & ' . $self->getLocal('genre') . ': & ' .
    $self->transformValue ($item->{genre}, 'genre') . '\\'
    if $item->{genre};
    $result .='\\ & ' . $self->getLocal('director') . ": & $item->{director}\\"
    if $item->{director};

    $result .='\\ & ' . $self->getLocal('time') . ": & $item->{time} мин.\\\\" if $item->{time};
    $result .='\\ & ' . $self->getLocal('country') . ": & " if $item->{country};
    $result .= "\n\\end{tabular}\n";
    }
    I will be grateful if someone here kindly show me the way.
     
    Rudra Banerjee, Dec 26, 2012
    #1
    1. Advertising

  2. Rudra Banerjee

    J. Gleixner Guest

    On 12/26/12 07:57, Rudra Banerjee wrote:
    > Dear friends,
    > I am trying to write a gcstar export for Latex, and I am a perl-illiterate.
    > From other gcstar scripts, I am following the pattern.
    > The gcstar structure is like:
    >
    > <item
    > id="1"
    > title="A Beautiful Mind"
    > date="2001"
    > time="130 min."
    > director="Ron Howard"
    > image="Movies_pictures/A_Beautiful_Mind_0.jpg"
    > backpic=""
    > original="A Beautiful Mind"
    > webPage="http://www.filmaffinity.com/en/film326587.html##Film affinity (EN)"
    > >

    > <country>
    > <line>
    > <col>United States</col>
    > </line>
    > </country>
    > <genre>
    > <line>
    > <col>Drama</col>
    > </line>
    > </genre>
    > </item>


    Looks like XML, so I'd suggest using one of the many XML parsers to
    parse and/or access your data. XML::Simple, XML:XPath, XML::LibXML, etc.

    >
    > Now, the other fields with key value structure is done, but I don't understand how to get things like
    > <country>
    > <line>
    > <col>United States</col>
    > </line>
    > </country>
    >
    > My complete code for the export may be quite too large, but for a snippet, what I am trying to do is as:
    > sub getItem {
    > my ($self, $item, $number) = @_;


    Include the structure of $item.

    use Data::Dumper;
    print Dumper( $item );

    [...]

    > I will be grateful if someone here kindly show me the way.


    After you look at the data structure for $item, it should
    make it pretty clear how to get the value you're after.

    A guess would be:

    print "Country=", $item->{ 'country' }{ 'line' }{ 'col' }, "\n";
     
    J. Gleixner, Dec 26, 2012
    #2
    1. Advertising

  3. my $event = q[

    <item
    id="1"
    title="A Beautiful Mind"
    date="2001"
    time="130 min."
    director="Ron Howard"
    image="Movies_pictures/A_Beautiful_Mind_0.jpg"
    backpic=""
    original="A Beautiful Mind"
    webPage="http://www.filmaffinity.com/en/film326587.html##Film affinity (EN)"
    >

    <country>
    <line>
    <col>United States</col>
    </line>
    </country>
    <genre>
    <line>
    <col>Drama</col>
    </line>
    </genre>
    </item>

    ];



    use XML::Simple;
    my $xml = XMLin($event);
    print $xml->{id} ,"\n";
    print $xml->{title} ,"\n";
    print $xml->{date} ,"\n";
    print $xml->{time} ,"\n";
    print $xml->{director} ,"\n";
    print $xml->{image} ,"\n";
    print $xml->{backpic} ,"\n";
    print $xml->{original} ,"\n";
    print $xml->{webPage} ,"\n";
    print $xml->{country}{line}{col} ,"\n";
    print $xml->{genre }{line}{col} ,"\n";
     
    George Mpouras, Dec 26, 2012
    #3
    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. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    757
    Martin Ambuhl
    Oct 26, 2004
  2. Patrick Kowalzick
    Replies:
    5
    Views:
    477
    Patrick Kowalzick
    Mar 14, 2006
  3. pereges
    Replies:
    79
    Views:
    1,569
    Keith Thompson
    Jun 19, 2008
  4. Davy
    Replies:
    2
    Views:
    141
  5. c like structure in Perl

    , Oct 24, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    285
    Peter J. Holzer
    Oct 24, 2006
Loading...

Share This Page