parse with multiple delimiter

A

anitawa

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
 
M

Mirco Wahab

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

Josef Moellers


What a coincidence!
Another poster, who imposes as "(e-mail address removed)" 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.
 
B

Brian McCauley

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;
 

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

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top