Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > parse with multiple delimiter

Reply
Thread Tools

parse with multiple delimiter

 
 
anitawa@gmail.com
Guest
Posts: n/a
 
      04-25-2007
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

 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      04-25-2007
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.



 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      04-26-2007
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

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      04-26-2007
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:umper;

$_='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;

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
optparse: parse v. parse! ?? 7stud -- Ruby 3 02-20-2008 05:20 AM
Processing multiple delimiter files ollie Perl Misc 17 03-14-2006 07:48 PM
How to parse a string like C program parse the command line string? linzhenhua1205@163.com C Programming 19 03-15-2005 07:41 PM
Parse Array of Objects - string delimiter java Pedro Rocha Java 8 12-08-2004 08:54 AM
Have perl increment a number that shows up before a delimiter john brown Perl 6 10-22-2003 09:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57