import array like structure using perl

R

Rudra Banerjee

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

J. Gleixner

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";
 
G

George Mpouras

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";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top