For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > April 2007 > parse with multiple delimiter









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author parse with multiple delimiter
anitawa@gmail.com

2007-04-25, 7:02 pm

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

Mirco Wahab

2007-04-25, 7:02 pm

anitawa@gmail.com 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.



Josef Moellers

2007-04-26, 7:03 pm

anitawa@gmail.com wrote:
> Hi,


What a coincidence!
Another poster, who imposes as "anitawa@gmail.com" posted exactly the=20
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=20
this person how the answers given helped.
--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

Brian McCauley

2007-04-26, 7:03 pm

On Apr 25, 8:40 pm, anit...@gmail.com 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;

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com