I wanna write a Perl script that searches into a .txt file

Discussion in 'PHP' started by elum.chaitu, Sep 28, 2010.

  1. #1
    I wanna write a Perl script that searches into a .txt file and for each number it finds, applies a certain conversion to that :

    Let say you have a txt file that contains such information inside :
    Code: 0 700000 HR 700000 1300000 H3 1300000 2100000 H2 2100000 3800000 HH 3800000 4500000 HQ 4500000 5000000 HP 5000000 5700000 HO

    Now, I want to multiply all the numbers found by a fix number (e.g r=1.5). So for example the first value after conversion will be :
    Code: 0*1.5=0;

    The second :
    Code: 700000*1.5=1050000

    HR will not be changed and so on. How can I do this please?

    Notes : the numbers are real numbers (not necessarily integer)
    Please in you code provide txt parsing as well.
     
    elum.chaitu, Sep 28, 2010 IP
  2. Media Signal

    Media Signal Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    sub is_integer
    {
       defined $_[0] && $_[0] =~ /^[+-]?\d+$/;
    }
    
    sub is_float
    {
       defined $_[0] && $_[0] =~ /^[+-]?\d+(\.\d+)?$/;
    }
    
    $dataFile = "data.txt";
    
    $conversationRatio = 1.5;
    
    open(DATA, $dataFile);
    
    @data = split(/ /, <DATA>);
    
    foreach (@data)
    {
        if (is_integer($_) || is_float($_)) {
            print $_, " - number - converted to ", ($_ * $conversationRatio), "<br>";
        } else {
            print $_, " - string <br>";
        }
    }
    Code (markup):
    I believe someone will be able to come up with a cleaner version but I hope you'll benefit from all of them.
     
    Media Signal, Sep 28, 2010 IP