exit if item >10

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

  1. 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?
    Rudra Banerjee, Dec 26, 2012
    #1
    1. Advertising

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

    print $#actors >= 10 ? 'too many' : @actors
    George Mpouras, Dec 26, 2012
    #2
    1. Advertising

  3. 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
    George Mpouras, Dec 26, 2012
    #3
  4. "George Mpouras"
    <> wrote:
    >print $#actors >= 10 ? 'too many' : @actors


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

    jue
    Jürgen Exner, Dec 26, 2012
    #4
  5. I think that $#array is faster than scalar(@array) - 1 because scalar is
    counting while $# points to the last offset



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

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


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

    jue
    George Mpouras, Dec 26, 2012
    #5
  6. [It wasn't me who messed up the quoting levels and did a TOFU]

    "George Mpouras"
    <> wrote:
    >I think that $#array is faster than scalar(@array) - 1 because scalar is
    >counting while $# points to the last offset
    >
    >? "Jurgen Exner" ?????? ??? ??????
    >news:...
    >
    >"George Mpouras"
    ><> 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
    Jürgen Exner, Dec 26, 2012
    #6
  7. 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!
    Rudra Banerjee, Dec 26, 2012
    #7
  8. Rudra Banerjee

    J. Gleixner Guest

    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
    J. Gleixner, Dec 26, 2012
    #8
  9. "George Mpouras"
    <>
    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.
    Rainer Weikusat, Dec 27, 2012
    #9
  10. wow !


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

    "George Mpouras"
    <>
    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.
    George Mpouras, Dec 28, 2012
    #10
    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. =?Utf-8?B?U2FuZHk=?=

    Code to Exit Web App and Exit Internet Explorer

    =?Utf-8?B?U2FuZHk=?=, Aug 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    7,895
    =?Utf-8?B?U2FuZHk=?=
    Aug 5, 2005
  2. Joe Smith
    Replies:
    4
    Views:
    65,836
    sandeep1976
    Nov 8, 2006
  3. Replies:
    2
    Views:
    452
    Jeff Epler
    May 31, 2005
  4. QQ
    Replies:
    5
    Views:
    517
    Jonathan Adams
    May 10, 2005
  5. Vicky

    Difference between exit(0) & exit (1)

    Vicky, Aug 8, 2006, in forum: C Programming
    Replies:
    6
    Views:
    706
    Kenneth Brody
    Aug 8, 2006
Loading...

Share This Page