49 users online (0 members and 49 guests)  

Thread: parsing string


  Results 1 to 2 of 2

Related

  1. Replies: 5
  2. Parsing XML file using XML::LIbXML    Forum: CGI Perl Forum
    Replies: 0
  3. Help parsing html for URLs    Forum: CGI Perl Forum
    Replies: 0
  4. Parsing javascript variable to PHP    Forum: PHP Forum
    Replies: 0
  5. Parsing javascript variable to PHP    Forum: Javascript Forum
    Replies: 0
  1. #1
    mdalsant's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2007
    Posts
    1

    parsing string

    Hi All,
    I have a string ->

    $line = 'total: 45 | total: 63'

    I need to parse the two integers from that string and ultimately determine the larger of the two... I can handle the simple max() functionality, but does anyone know how to extract the two integers into, lets say, $num1 = 45 and $num2 = 63.

    Thanks in advance,
    MP

  2. #2
    xmassey's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Apr 2007
    Posts
    100

    Re: parsing string

    Hello, yes this isn't so difficult..
    First of all don't wrap it in ' ', but " " instead else its taken literally, just a good habit
    $line = "total: 45 | total: 63"
    ### FIRST REMOVE THE WHILE SPACE SO WE ARE LEFT WITH "total:45|total:63"
    $line =~ s/ //gi;
    ### NOW SPLIT THE 2 PARTS (SPLIT USING THE |) SO WE ARE LEFT WITH "total:45" and "total:63"
    @split = split(/\|/, $line);
    ### SPLIT EACH AGAIN USING THE : AND PASS NUMBER VALUE TO $

    $counter = 1;
    foreach (@split) {
    @one = split(/\:/, $_);
    @numbers = push($one[$counter]);
    $counter++
    }
    $your_first_number = $numbers[1];
    $your_second_number = $numbers[2];

    I onl just wrote this quick, so excuse any syntax errors if there are any

    Chris



Tags for this Thread