parse with multiple delimiter

Discussion in 'Perl Misc' started by anitawa@gmail.com, Apr 25, 2007.

  1. Guest

    Hi,

    I am new to perl and trying to parse a text file into a hash table.
    Could someone point me in the right direction?

    My text file:
    start -days 1,2,3,4,5 -month 2,4,8,10 -message "I want you to start
    this" -running -group "rungroup"

    Hash table should look like this:

    myhash(start) = ""
    myhash(days) = 1,2,3,4,5
    myhashh(month) = 2,4,8,10
    myhash(message) = "I want you to start this"
    myhash(running) = ""
    myhash(group) = "rungroup"

    Thanks
    , Apr 25, 2007
    #1
    1. Advertising

  2. Mirco Wahab Guest

    wrote:
    > I am new to perl and trying to parse a text file into a hash table.
    > Could someone point me in the right direction?
    > My text file:
    > start -days 1,2,3,4,5 -month 2,4,8,10 -message "I want you to start
    > this" -running -group "rungroup"
    >
    > Hash table should look like this:
    >
    > myhash(start) = ""
    > myhash(days) = 1,2,3,4,5
    > myhashh(month) = 2,4,8,10
    > myhash(message) = "I want you to start this"
    > myhash(running) = ""
    > myhash(group) = "rungroup"


    You could match and map the entries then into a hash, like:

    ==>
    ...
    my $line='start -days 1,2,3,4,5 -month 2,4,8,10 -message "I want you to start this" -running -group "rungroup"';
    my $id = 0;

    my %myhash = map +($id++ & 1) && /^-/ ? (++$id && '""', $_) : $_,
    $line =~ /((?:".+?")|\S+)/g;

    print "myhash($_) = $myhash{$_}\n" for keys %myhash;
    ...
    <==

    The trick is to count the splitted string
    elements and look if the '-' option shows
    up on "odd positions" (therefore the ++$id)

    Regards

    M.
    Mirco Wahab, Apr 25, 2007
    #2
    1. Advertising

  3. wrote:
    > Hi,


    What a coincidence!
    Another poster, who imposes as "" posted exactly the
    same question, but with a different subject "How to parse text file into
    hash table" only two days ago! Maybe you know him/her, so maybe you ask
    this person how the answers given helped.
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Apr 26, 2007
    #3
  4. On Apr 25, 8:40 pm, wrote:
    > Hi,
    >
    > I am new to perl and trying to parse a text file into a hash table.
    > Could someone point me in the right direction?
    >
    > My text file:
    > start -days 1,2,3,4,5 -month 2,4,8,10 -message "I want you to start
    > this" -running -group "rungroup"
    >
    > Hash table should look like this:
    >
    > myhash(start) = ""
    > myhash(days) = 1,2,3,4,5
    > myhashh(month) = 2,4,8,10
    > myhash(message) = "I want you to start this"
    > myhash(running) = ""
    > myhash(group) = "rungroup"


    Please try to use Perl notation in your posts to Perl newsgroups.

    $myhash{group} = "rungroup"

    Your task shouldn't be too difficult but we need to know a couple of
    things:

    Where did the empty string in $myhash{start} come from?

    What are the rules about quotes? For now I'll assume there can be no
    quote characters in the data.

    Do you want to validate the data or can we assume the input fits the
    pattern?

    use strict;
    use warnings;
    use Data::Dumper;

    $_='start -days 1,2,3,4,5 -month 2,4,8,10 -message "I want you to
    start this" -running -group "rungroup"';

    my %myhash = /\G(?:^|-)(\w+)\s+("[^"]*"|(?=-)|[^" ]*)\s*/g;

    tr/"//d for values %myhash;

    print Dumper \%myhash;
    Brian McCauley, Apr 26, 2007
    #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. john brown
    Replies:
    6
    Views:
    572
    Jim Gibson
    Oct 22, 2003
  2. Pedro Rocha
    Replies:
    8
    Views:
    15,379
    Boudewijn Dijkstra
    Dec 8, 2004
  3. Replies:
    19
    Views:
    1,119
    Daniel Vallstrom
    Mar 15, 2005
  4. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    183
    7stud --
    Feb 20, 2008
  5. ollie
    Replies:
    17
    Views:
    167
    Tad McClellan
    Mar 14, 2006
Loading...

Share This Page