extract words from string

Discussion in 'Perl Misc' started by Jack S, Jul 14, 2004.

  1. Jack S

    Jack S Guest

    Hi, i'm playing with perl for my first time and want to use it to extract
    some words from a string.
    How the string looks:
    PrimeraDiv 05.09.1993 Ath._Bilbao Albacete 4-1 ( - )

    Or:
    PrimeraDiv 23.05.2004 Zaragoza Barcelona 2-1 ( 1-1 )

    Now, i want to extract the second word, in this case a date.
    The third and forth words(team names). The result and the half-time result
    (between parentesis). Sometimes half-time result isn't available and i want
    to replace it with the string "NA".
    I know how to get the data from the file(wich is where the strings are
    located):

    open(LOG, "c:\\SPA_1993-1994.txt");
    @indata = <LOG>;

    But how can i use reg. expressions to match the words i told you? and most
    important how can i extract them?

    Thanks
     
    Jack S, Jul 14, 2004
    #1
    1. Advertising

  2. Jack S

    Paul Lalli Guest

    On Wed, 14 Jul 2004, Jack S wrote:

    > Hi, i'm playing with perl for my first time and want to use it to extract
    > some words from a string.
    > How the string looks:
    > PrimeraDiv 05.09.1993 Ath._Bilbao Albacete 4-1 ( - )
    >
    > Or:
    > PrimeraDiv 23.05.2004 Zaragoza Barcelona 2-1 ( 1-1 )
    >
    > Now, i want to extract the second word, in this case a date.
    > The third and forth words(team names). The result and the half-time result
    > (between parentesis). Sometimes half-time result isn't available and i want
    > to replace it with the string "NA".
    > I know how to get the data from the file(wich is where the strings are
    > located):
    >
    > open(LOG, "c:\\SPA_1993-1994.txt");
    > @indata = <LOG>;
    >
    > But how can i use reg. expressions to match the words i told you? and most
    > important how can i extract them?


    When you say "word" here, you really mean "white-space delimited fields",
    yes? I would use a simple split.

    (undef, $date, $team1, $team2, $score, undef, $half) = split / /, $string;
    $half = "NA" unless $half =~ /\d/; #replace halftime if no numbers.

    The first undef accounts for the first field you want to throw away
    (PrimeraDiv). The second accounts for the opening paren you want to throw
    away.

    perldoc -f split
    for more info on the split function

    and you should probably read up on regular expressions:
    perldoc perlre

    Paul Lalli
     
    Paul Lalli, Jul 14, 2004
    #2
    1. Advertising

  3. Jack S

    Tore Aursand Guest

    On Wed, 14 Jul 2004 15:38:47 +0100, Jack S wrote:
    > How the string looks:
    >
    > PrimeraDiv 05.09.1993 Ath._Bilbao Albacete 4-1 ( - )
    > PrimeraDiv 23.05.2004 Zaragoza Barcelona 2-1 ( 1-1 )
    >
    > Now, i want to extract the second word, in this case a date.
    > The third and forth words(team names). The result and the half-time result
    > (between parentesis). Sometimes half-time result isn't available and i want
    > to replace it with the string "NA".


    This should get you started (untested);

    #!/usr/bin/perl
    #
    use strict;
    use warnings;

    open( LOG, '<', 'c:\SPA_1993-1994.txt' ) or die "$!\n";
    while ( <LOG> ) {
    chomp;
    my ( $div, $date, $home, $away, $fulltime, $halftime ) = split( /\s+/, $_, 6 );
    # ...
    }
    close( LOG );

    Not really anything you need to spend regular expressions on.


    --
    Tore Aursand <>
    "Those people who think they know everything are a great annoyance to
    those of us who do." (Isaac Asimov)
     
    Tore Aursand, Jul 14, 2004
    #3
  4. Jack S

    Jack S Guest

    Thanks, both solutions worked just fine :)
     
    Jack S, Jul 14, 2004
    #4
    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. Peter Strøiman
    Replies:
    1
    Views:
    2,092
    Peter Strøiman
    Aug 23, 2005
  2. Richard Heathfield
    Replies:
    7
    Views:
    366
    Barry Schwarz
    Oct 5, 2003
  3. utab

    Words Words

    utab, Feb 16, 2006, in forum: C++
    Replies:
    6
    Views:
    429
    Daniel T.
    Feb 16, 2006
  4. BerlinBrown
    Replies:
    6
    Views:
    4,508
  5. pantagruel
    Replies:
    8
    Views:
    453
    Dr John Stockton
    Jul 22, 2006
Loading...

Share This Page