Professional Web Applications Themes

how to extract digit from a line in Perl - PERL Beginners

Hello: I want to extract the digit from a line of text. For exmple, for a text line: TSC2101Net, how can I write a script to extract 2101 and print it? Thank you very much in advance! Bestwishes Franklin...

  1. #1

    Default how to extract digit from a line in Perl

    Hello:

    I want to extract the digit from a line of text. For exmple, for a text
    line: TSC2101Net, how can I write a script to extract 2101 and print it?

    Thank you very much in advance!

    Bestwishes
    Franklin
    Franklin Guest

  2. #2

    Default Re: how to extract digit from a line in Perl

    On 14 Jul 2004, at 11:00, Franklin wrote: 

    There are number of techniques that spring to mind, the obvious being
    regular expressions and substring. The latter isn't much good unless
    you know the position of the numbers in the string. The former is more
    flexible but also more complex.

    perldoc perlrequick
    perldoc -f substr
     

    Evolved into a distracting blinking thing that gives me a headache.
    Eugh.

    --
    David Dorward
    <http://dorward.me.uk/>
    <http://blog.dorward.me.uk/>
    David Guest

  3. #3

    Default Re: how to extract digit from a line in Perl

    Franklin wrote:
     

    try this [take your own risk :) ] :

    ******cut here*****************
    #!/usr/bin/perl
    #

    $bannerkoe=<<END
    Tes regex
    :))

    END
    ;
    print "$bannerkoe";

    $phile="tes_regex2.txt";

    open(FH, $phile) || die("Cannot open the file");
    fh=<FH>;

    foreach $bingung(fh) {
    pecah=split/ /,$bingung;
    foreach (pecah) {
    kar=split//,$_;
    foreach $elemen(kar) {
    $hasil=$elemen=~m/(\d+)/;
    if ($hasil) {print "$elemen";}
    }
    }
    print "\nasalnya : $bingung\n";
    }

    close(FH);
    ****cut here**********

    the content of tes_regex2.txt :
    aku tes aja ya...
    TSC2101Net
    :)



    Iko Riyadi
    Departemen Data Manajemen
    PT. Infomedia Nusantara
    Jl. Kusuma Bangsa 10-12
    Surabaya 60272 Indonesia
    Telp: +62 (031) 5326282, Fax: +62 (031) 5343613
    http://www.yellowpages.co.id
    Iko Guest

  4. #4

    Default RE: how to extract digit from a line in Perl

    Franklin wrote: 

    Use a regex to capture a series of digits:

    $_ = 'TSC2101Net';
    my ($num) = /(\d+)/;
    print "Found $num\n" if defined $num;

    This will simply find the leftmost sequence of one or more digits in the
    string.

    HTH
    Bob Guest

  5. #5

    Default Data File getting set to an empty file

    Hi all,

    I have a (possible) problem with a script that records the number of page
    views for various pages on a site (full script below).

    Every so often (60 days or more) the data file (data.xml) loses all it's
    data and is still there but empty.

    Any suggestions greatly appreciated.

    Cheers,
    Nigel

    -=-=-=-=-=Script Follows=-=-=-=-=-

    use strict;
    use XML::LibXML;
    use CGI;
    use IO::AtomicFile;

    sub build_dom (\$\$\$);
    sub commit_changes ($$\$);
    sub error ($);

    my $query = new CGI;
    $CGI::DISABLE_UPLOADS = 1;

    print "Content-type: text/html\n\n";

    my $lasterror;
    my $data_file = "/home/sebs/pageviews/data.xml";
    my $error_file = "/home/sebs/pageviews/errors.txt";

    my $location = $query->param("location") ||
    error ('Required variable location was not sent');

    my $data;

    build_dom($data, $data_file, $lasterror)
    || error($lasterror);

    my $views;
    my $node = (($data->findnodes('//page[location="' . $location .
    '"]'))?($data->findnodes('//page[location="' . $location . '"]')->[0]):'');

    if ( not $node ) {
    # It's a new page, not seen before, create a page element for it
    $node = XML::LibXML::Element->new('page');
    $node->setAttribute('location', $location);
    $node->setAttribute('views', 0);
    $node->setAttribute('starttime', time);
    $data->getDoentElement->appendChild($node);
    }

    my $views = $node->getAttribute('views');

    if ( $query->param("average") == 1 ) {
    # Send the average and die if average was set
    my $starttime = $node->getAttribute('starttime');
    my $timediff = time - $starttime;
    # Seconds in a 30 day month
    # 2592000
    my $months = int($timediff / 2592000) + 1;
    my $average = $views / $months;
    print int($average);
    die;
    }

    unless ($query->param("noview") == 1) {
    # Increase views unless noview was set
    $views++;
    $node->setAttribute('views', $views);

    commit_changes($data, $data_file, $lasterror)
    || error($lasterror);
    }

    print $views;

    ##########################
    # COMMON SUBS #
    ##########################

    sub error ($) {
    my ($errormsg) = _;
    print "<h2>Error</h2>\n";
    print "<p>Please note the information below, this was the cause of the
    error.</p>\n";
    print "<p>" . $errormsg . "</p>\n";
    open ( ERRORS, ">>$error_file" );
    print ERRORS $errormsg . "\n";
    close ( ERRORS );
    die;
    }

    ##########################
    # DOM STUFF #
    ##########################

    sub build_dom (\$\$\$) {
    my ($doc, $config, $errormsg) = _;

    my $pr = XML::LibXML->new();
    $pr->validation(0);
    $pr->expand_entities(1);
    $pr->keep_blanks(0);
    $pr->pedantic_pr(0);
    $pr->load_ext_dtd(0);
    $pr->complete_attributes(1);

    eval { $$doc = $pr->p_file( $$config ) };
    if ($) { $$errormsg = "build_dom error: " . $ . "\n"; return 0; }
    else { return 1; }
    }

    sub commit_changes ($$\$) {
    my ($doc, $file, $errormsg) = _;
    my $fh;
    unless ($fh = IO::AtomicFile->open($file,"w")) { $$errormsg = "Couldn't
    open filehandle to $file : $!";
    return 0; }

    eval { $doc->toFH($fh, 1) };
    if ($) { $fh->delete();
    $$errormsg = "commit_changes error with $file : " . $ . "\n";
    return 0;
    } elsif ( not $fh->close() ) { $$errormsg = "Couldn't write file $file";
    } else { return 1 }
    }

    -=-=-=-=-=Script Ends=-=-=-=-=-

    Nigel Guest

  6. #6

    Default Re: Data File getting set to an empty file

    From: "Nigel Peck" <com> 

    Waitasecond ... all your CGI scripts try to edit a potentially huge
    XML file? I must be missing something here!

    XML might be a fine format for exchanging information, but as an
    editable database it's almost the worst thing you could get.

    No matter how good is the XML pr it takes ages to find the
    information and even longer to change the XML.

    You should be using a database for this.

    If you do not want to install any "real" one try DBD::SQLite. You'll
    find out the modifications are much much quicker.

    Jenda
    P.S.: Guess what happens if two instances of your script are started
    at the same time, both read the same XML, both make some changes and
    both write their changes to the file? Yes the write is "atomic", but
    there is nothing in the script preventing one instance from reading
    the XML between the other reads it and writes it!

     


    ===== cz === http://Jenda.Krynicky.cz =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery

    Jenda Guest

  7. #7

    Default RE: Data File getting set to an empty file

    Thanks for all that, any suggestions why the file gets wiped out every so
    often?

    -----Original Message-----
    From: Jenda Krynicky [mailto:cz]
    Sent: 14 July 2004 21:06
    To: org
    Subject: Re: Data File getting set to an empty file


    From: "Nigel Peck" <com> 

    Waitasecond ... all your CGI scripts try to edit a potentially huge
    XML file? I must be missing something here!

    XML might be a fine format for exchanging information, but as an
    editable database it's almost the worst thing you could get.

    No matter how good is the XML pr it takes ages to find the
    information and even longer to change the XML.

    You should be using a database for this.

    If you do not want to install any "real" one try DBD::SQLite. You'll
    find out the modifications are much much quicker.

    Jenda
    P.S.: Guess what happens if two instances of your script are started
    at the same time, both read the same XML, both make some changes and
    both write their changes to the file? Yes the write is "atomic", but
    there is nothing in the script preventing one instance from reading
    the XML between the other reads it and writes it!

     


    ===== cz === http://Jenda.Krynicky.cz =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery


    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>


    Nigel Guest

Similar Threads

  1. Query 9-digit zipcodes against a 5-digit list
    By yoman in forum Coldfusion Database Access
    Replies: 4
    Last Post: February 5th, 02:33 AM
  2. extract last digit from the name of an instance
    By nowB@ webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 1
    Last Post: January 23rd, 01:30 PM
  3. extract las digit in the name of an instance
    By feiloiram webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 3
    Last Post: January 23rd, 10:41 AM
  4. using perl on the command line, like sed or awk
    By gorda in forum PERL Miscellaneous
    Replies: 2
    Last Post: October 21st, 01:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139