How to parse text file into hash table

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

  1. Guest

    Hi,

    I am new to perl and i need some help.
    Basically i have some text file like this:

    start -day 1,2,3 -month 6,12 -message "there is a message for you" -
    group book club -emergency

    I want to put this into a hash table like this

    myhash(start) = ""
    myhash(day) = "1,2,3"
    myhash(month) = "6,12"
    myhash(message) = "there is a message for you"
    myhash(group) = "book club"
    myhash(emergency) = ""


    Can someone help me get started?
     
    , Apr 24, 2007
    #1
    1. Advertising

  2. <> wrote:


    > I am new to perl and i need some help.



    A hash access starts with a dollar sign.

    A hash subscript goes in curly braces.

    Statements in Perl are separated with a semicolon.


    > myhash(start) = ""
    > myhash(day) = "1,2,3"



    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 25, 2007
    #2
    1. Advertising

  3. <> wrote:


    > Basically i have some text file like this:
    >
    > start -day 1,2,3 -month 6,12 -message "there is a message for you" -
    > group book club -emergency



    Please speak Perl rather than English, when possible.

    If you had (as I do below) then everybody knows what your real
    data looks like (it appears your posting software word wrapped
    it in a bad place).

    Have you seen the Posting Guidelines that are posted here frequently?



    Your data is inconsistent as the first key-to-be does not start
    with a hyphen like all of the other ones do, so I added a hyphen
    below.


    > I want to put this into a hash table like this
    >
    > myhash(start) = ""
    > myhash(day) = "1,2,3"
    > myhash(month) = "6,12"
    > myhash(message) = "there is a message for you"
    > myhash(group) = "book club"
    > myhash(emergency) = ""
    >
    >
    > Can someone help me get started?



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

    my $str = '-start -day 1,2,3 -month 6,12 -message "there is a message '
    . 'for you" -group book club -emergency';

    my %myhash = $str =~ /(-\S+)\s*([^-]*)/g;

    print qq($_ ==> "$myhash{$_}"\n) for sort keys %myhash;
    -----------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 25, 2007
    #3
  4. Mirco Wahab Guest

    Tad McClellan wrote:
    > <> wrote:
    >> Basically i have some text file like this:
    >> start -day 1,2,3 -month 6,12 -message "there is a message for you" -
    >> group book club -emergency
    >> Can someone help me get started?

    > -----------------------------
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > my $str = '-start -day 1,2,3 -month 6,12 -message "there is a message '
    > . 'for you" -group book club -emergency';
    >
    > my %myhash = $str =~ /(-\S+)\s*([^-]*)/g;
    >
    > print qq($_ ==> "$myhash{$_}"\n) for sort keys %myhash;
    > -----------------------------


    Doooh!

    After I answered the OP's question further below
    I found this one here which is almost identical.

    Now I was stunned how compact and elegant your
    solution is, you simply made an extra '-' in front
    of the 'out of sync' elements and ignored any "quoted"
    stuff completely (by just reading through).

    Nice!

    (but why in heaven did the OP repeat the same question then ?)

    Thanks & Regards

    M.
     
    Mirco Wahab, Apr 25, 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. rp
    Replies:
    1
    Views:
    555
    red floyd
    Nov 10, 2011
  2. Ernst Tanaka
    Replies:
    8
    Views:
    316
    Vitor Peres
    Dec 5, 2007
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    640
    David A. Black
    Jul 2, 2008
  4. batrams

    Parse using Text::CSV into Hash

    batrams, Nov 10, 2011, in forum: Perl Misc
    Replies:
    9
    Views:
    492
    Rainer Weikusat
    Nov 11, 2011
  5. Zhen Zhang

    parse a csv file into a text file

    Zhen Zhang, Feb 6, 2014, in forum: Python
    Replies:
    29
    Views:
    141
    Tim Chase
    Feb 6, 2014
Loading...

Share This Page