How to parse text file into hash table

A

anitawa

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?
 
T

Tad McClellan

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.
 
T

Tad McClellan

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;
 
M

Mirco Wahab

Tad said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top