FAQ

-----Original Message-----
From: V.Ramkumar
Sent: 12 September 2008 11:33
To: beginners@perl.org
Subject: Round a digit in perl

Hi List,

My input xml file has,
<citspn>246</citspn><delim>&ndash;</delim><citepn>52</citepn>

I have to replace,
<citspn>246</citspn><delim>&ndash;</delim><citepn>252</citepn>

Similarly,

100-5 100-105
198-10 198-210.
[Stewart Anderson]
Have a look at XML::Smart it has an easy to use interface. You
could run through your xml elements and just prefix the elements
with the new code.

That might take a little longer than a regex but I feel it would be
safer and probably easier to predict the outcome. I just heard a
sharp intake of breath from regex kings across the world :) That's my
view - not being a regex king!!



Information in this email including any attachments may be privileged, confidential and is intended exclusively for the addressee. The views expressed may not be official policy, but the personal views of the originator. If you have received it in error, please notify the sender by return e-mail and delete it from your system. You should not reproduce, distribute, store, retransmit, use or disclose its contents to anyone. Please note we reserve the right to monitor all e-mail communication through our internal and external networks. SKY and the SKY marks are trade marks of British Sky Broadcasting Group plc and are used under licence. British Sky Broadcasting Limited (Registration No. 2906991), Sky Interactive Limited (Registration No. 3554332), Sky-In-Home Service Limited (Registration No. 2067075) and Sky Subscribers Services Limited (Registration No. 2340150) are direct or indirect subsidiaries of British Sky Broadcasting Group plc (Registration No. 2247735). All of the companies mentioned in this paragraph are incorporated in England and Wales and share the same registered office at Grant Way, Isleworth, Middlesex TW7 5QD.

Search Discussions

  • Raymond Wan at Sep 12, 2008 at 12:13 pm
    Hi Ramkumar,

    Ummm, as a suggestion, why don't you explain to us (in words) what you
    want to do and try some sample code and we can give you some
    feedback/corrections?

    Your question about whether anyone has any "logic for the above" sounds
    like you can't put the problem into words? That seems to be the first
    step in any problem? I mean, obviously, "citspn" is the citation start
    page number and "citepn" is the citation end page number...

    Ray



    V.Ramkumar wrote:
    Hi List,

    My input xml file has,
    <citspn>246</citspn><delim>&ndash;</delim><citepn>52</citepn>

    I have to replace,
    <citspn>246</citspn><delim>&ndash;</delim><citepn>252</citepn>

    Similarly,

    100-5 100-105
    198-10 198-210.

    If anybody have logic for the above, please suggest.
  • Thomas Bätzler at Sep 12, 2008 at 12:33 pm
    Hi,

    V.Ramkumar <V.Ramkumar@macmillansolutions.com> asked:
    My input xml file has,
    <citspn>246</citspn><delim>&ndash;</delim><citepn>52</citepn>

    I have to replace,
    <citspn>246</citspn><delim>&ndash;</delim><citepn>252</citepn>

    Similarly,

    100-5 100-105
    198-10 198-210.

    If anybody have logic for the above, please suggest.
    Hope this helps:

    #!/usr/bin/perl -w

    $| = 1;

    use strict;

    sub correct_range {
    my @args = @_; # nasty side effects if you map @_!
    my( $from, $to ) = map { $_ = reverse $_ } @args;

    substr( $from, 0, length $to ) = $to;

    return reverse( $from );
    }


    while( my $line = <DATA> ){
    my( $from, $to ) = split /\s+/, $line;
    print "In: $from -- $to\n";
    $to = correct_range( $from, $to );
    print "Out: $from -- $to\n\n";

    }

    __DATA__
    1 17
    201 2
    123 45
    199 201
    999 1002

    HTH,
    Thomas
  • V.Ramkumar at Sep 12, 2008 at 12:48 pm
    Hope this helps:
    #!/usr/bin/perl -w
    $| = 1;
    use strict;
    sub correct_range {
    my @args = @_; # nasty side effects if you map @_!
    my( $from, $to ) = map { $_ = reverse $_ } @args;
    substr( $from, 0, length $to ) = $to;
    return reverse( $from );
    }
    while( my $line = <DATA> ){
    my( $from, $to ) = split /\s+/, $line;
    print "In: $from -- $to\n";
    $to = correct_range( $from, $to );
    print "Out: $from -- $to\n\n"
    }
    Thanks a lot dear Thomas. I got the expected output.

    Regards,
    Ramkumar

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedSep 12, '08 at 12:08p
activeSep 12, '08 at 12:48p
posts4
users4
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase