×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Data parsing

Data parsing

Data parsing

(OP)
id:2 name:test1 longname:test2 with space longname description:here is my description.id:10 name:test3 longname:test 4 long name description:a discription of test 5


I have data like above and I'd like to get it into a hash like
%hash { 'id:2' =>
{name => test1
longname => test2 with space longname
description => here is my description.},
'id:10' =>
{name => test3
longname => test 4 long name
description: a discription of test 5
}
}


I have tried a multitude of regex/splits and can't quit get it to split the way I want it to..

Thanks in advance


Travis

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
noevil
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;

RE: Data parsing

A suggestion to start with:

CODE

@fields=split/(id:|name:|longname:|description:)/;
shift@fields;
while($k=shift@fields){
  $v=shift@fields;
  $v=~s/\s+$//;
  if($k eq'id:'){
    $mainkey='id:'.$v;
  }else{
    $hash{$mainkey}{substr($k,0,-1)}=$v;
  }
} 

http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: Data parsing

(OP)
Thanks.. I was struggling with that one for a bit, I figured I could figure out a split that would pick up the word before the : and then everything up to the word before the next :, but it was just killing me.... your seems so simple :)

Thanks!!!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
noevil
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;

RE: Data parsing

If you wanted to use a regular expression....

CODE

#!perl

my $line = ('id:2 name:test1 longname:test2 with space longname description:here is my description.id:10 name:test3 longname:test 4 long name description:a discription of test 5');

my %hash;
while ($line =~ /id:(\d+)\s+name:(.*?)\s+longname:(.*?)\s+description:(.*?)(?=id:|$)/g ){
    $hash{id} = $1;
    $hash{name} = $2;
    $hash{longname} = $3;
    $hash{description} = $4;
print "\n";
foreach $key (keys(%hash)) { print "$key: $hash{$key}\n"; }
} 

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close