Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > exit if item >10

Reply
Thread Tools

exit if item >10

 
 
Rudra Banerjee
Guest
Posts: n/a
 
      12-26-2012
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?

 
Reply With Quote
 
 
 
 
George Mpouras
Guest
Posts: n/a
 
      12-26-2012

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

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



 
Reply With Quote
 
 
 
 
George Mpouras
Guest
Posts: n/a
 
      12-26-2012
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



 
Reply With Quote
 
Jrgen Exner
Guest
Posts: n/a
 
      12-26-2012
"George Mpouras"
<(E-Mail Removed) m.com.nospam> wrote:
>print $#actors >= 10 ? 'too many' : @actors


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

jue
 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      12-26-2012


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



Ο "Jurgen Exner" *γραψε στο μήνυμα
news:(E-Mail Removed)...

"George Mpouras"
<(E-Mail Removed) m.com.nospam> wrote:
>print $#actors >= 10 ? 'too many' : @actors


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

jue

 
Reply With Quote
 
Jrgen Exner
Guest
Posts: n/a
 
      12-26-2012
[It wasn't me who messed up the quoting levels and did a TOFU]

"George Mpouras"
<(E-Mail Removed) m.com.nospam> wrote:
>I think that $#array is faster than scalar(@array) - 1 because scalar is
>counting while $# points to the last offset
>
>? "Jurgen Exner" ?????? ??? ??????
>news:(E-Mail Removed).. .
>
>"George Mpouras"
><(E-Mail Removed) am.com.nospam> wrote:
>>print $#actors >= 10 ? 'too many' : @actors

>
>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
 
Reply With Quote
 
Rudra Banerjee
Guest
Posts: n/a
 
      12-26-2012
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!
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      12-26-2012
On 12/26/12 15:44, Rudra Banerjee wrote:
> 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

 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      12-27-2012
"George Mpouras"
<(E-Mail Removed) m.com.nospam>
writes:
> 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.

 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      12-28-2012

wow !


Ο "Rainer Weikusat" *γραψε στο μήνυμα
news:(E-Mail Removed) om...

"George Mpouras"
<(E-Mail Removed) m.com.nospam>
writes:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exit code of a batch (using exit /B) Joe Smith Java 4 11-08-2006 12:25 PM
Difference between exit(0) & exit (1) Vicky C Programming 6 08-08-2006 09:11 PM
Code to Exit Web App and Exit Internet Explorer =?Utf-8?B?U2FuZHk=?= ASP .Net 7 08-05-2005 01:55 AM
exit after process exit ajikoe@gmail.com Python 2 05-31-2005 08:11 PM
What's the difference of return 0; exit(0);exit(1) QQ C Programming 5 05-10-2005 10:11 PM



Advertisments