exit if item >10

R

Rudra Banerjee

Hello friends,
I am a perl illiterate, but managed to write a code for gcstar(a package to create catalogue), to export to latex.
I have one little problem. problem is, for movies, the list of actors are written as
<actors>
<line>
<col>Russell Crowe</col>
<col></col>
</line>
<line>
<col>Jennifer Connelly</col>
<col></col>
</line>
<line>
<col>Ed Harris</col>
<col></col>
</line>
<line>
<col>Paul Bettany</col>
<col></col>
.......

and so on.
I have the code to extract these *all* Actors listed, i.e., between <actors> to </actors>.
The corresponding piece of code is:
$result .=' & \multirow{1}{4in}{' . $self->getLocal('actors') . ': \small{' .
$self->transformValue ($item->{actors}, 'actors') . '}} \\'
if $item->{actors};

what I am trying to do is to do a check like:
if ($item->{actors} || $item->{actors}>10);
so that not more then 10 actors name are included.
but this line has no effect. can you kindly help me with this?
 
G

George Mpouras

use XML::Simple;
my $xml = XMLin($event, ContentKey => 'col' );
my @actors = map {$_->{col}[0]} @{$xml->{line}};

print $#actors >= 10 ? 'too many' : @actors
 
G

George Mpouras

use XML::Simple;
my $xml = XMLin(q[

<actors>
<line>
<col>Russell Crowe</col>
<col></col>
</line>
<line>
<col>Jennifer Connelly</col>
<col></col>
</line>
<line>
<col>Ed Harris</col>
<col></col>
</line>
<line>
<col>Paul Bettany</col>
<col></col>
</line>
</actors>

]);
my @actors = map {$_->{col}[0]} @{$xml->{line}};

print $#actors >= 10 ? 'too many' : @actors
 
G

George Mpouras

I think that $#array is faster than scalar(@array) - 1 because scalar is
counting while $# points to the last offset



Ο "Jurgen Exner" έγÏαψε στο μήνυμα

"George Mpouras"
print $#actors >= 10 ? 'too many' : @actors

Didn't you mean
print (scalar @actors) > 10 ? 'too many' : @actors
maybe?

jue
 
J

Jürgen Exner

[It wasn't me who messed up the quoting levels and did a TOFU]

"George Mpouras"
I think that $#array is faster than scalar(@array) - 1 because scalar is
counting while $# points to the last offset

? "Jurgen Exner" ?????? ??? ??????

"George Mpouras"


Didn't you mean
print (scalar @actors) > 10 ? 'too many' : @actors
maybe?

Actually no. The length of an array is stored in the array directly and
can be accessed in O(1). No need for counting at all.
Plus $#array will give you the wrong result if someone was stupid enough
to mess with $[.

jue
 
R

Rudra Banerjee

oops....may be I was not clear enough, but the <actors>...</actors> are from gcstar, and I cant really do a "my $xml = XMLin(q[" here!
 
J

J. Gleixner

oops....may be I was not clear enough, but the<actors>...</actors> are from gcstar, and I cant really do a "my $xml = XMLin(q[" here!

Simply read the data from STDIN. Or if you have the output in a file,
then use the filename as the argument to XMLin().

Read the documentation for the XMLin method. It clearly documents
exactly how to do that and it's very 'simple'.

perldoc XML::Simple
 
R

Rainer Weikusat

"George Mpouras"
I think that $#array is faster than scalar(@array) - 1 because scalar
is counting while $# points to the last offset

Neither scalar(@array) nor $#array counts anything -- they are both
based on using the same internal 'highest-numbered used slot'
variable. As was discussed not that long ago, scalar(@array) is
actually faster because (in 'commonly-used perls') it doesn't need to
deal with the possible case of being assigned to and doesn't need to
take a possible 'base indexing displacement' into account (where it
still exists). When evaluated, $#array ends up pushing a 'magic
scalar' onto the stack which either evaluates to the corresponding
number or provides the 'change array size by assigning to $#array'
facility via assoicated 'magic' (aka a method table). In more recent
perls, this indirection isn't done anymore when the context of the
evaluation is such that an assignment can't happen, eg

$n = $#array

It is still necessary when $#array is used in an lvalue context. This
includes being passed as argument to a subroutine.
 
G

George Mpouras

wow !


Ο "Rainer Weikusat" έγÏαψε στο μήνυμα

"George Mpouras"
I think that $#array is faster than scalar(@array) - 1 because scalar
is counting while $# points to the last offset

Neither scalar(@array) nor $#array counts anything -- they are both
based on using the same internal 'highest-numbered used slot'
variable. As was discussed not that long ago, scalar(@array) is
actually faster because (in 'commonly-used perls') it doesn't need to
deal with the possible case of being assigned to and doesn't need to
take a possible 'base indexing displacement' into account (where it
still exists). When evaluated, $#array ends up pushing a 'magic
scalar' onto the stack which either evaluates to the corresponding
number or provides the 'change array size by assigning to $#array'
facility via assoicated 'magic' (aka a method table). In more recent
perls, this indirection isn't done anymore when the context of the
evaluation is such that an assignment can't happen, eg

$n = $#array

It is still necessary when $#array is used in an lvalue context. This
includes being passed as argument to a subroutine.
 

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

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top