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!

*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

Trying to avoid nested split commands when parsing a file

Trying to avoid nested split commands when parsing a file

Trying to avoid nested split commands when parsing a file

Although I have worked with Perl before I have not done any significant programming in it. I currently have a problem which seems like it should have a more elegant solution that I can come up with, but I am not familiar enough with parsing data to know.

I have a data file which I receive from a third party of the format:
[{“a”:”24”,”b”:”2014-01-15”,”c”:”English”,”d”:”3Y”}, {“a”:”21”,”b”:”2014-01-12”,”c”:”English”,”d”:”5Y”}, {“a”:”12”,”b”:”2013-12-15”,”c”:”French”,”d”:”7Y”}, {“a”:”18”,”b”:”2014-01-15”,”c”:”German”,”d”:”15Y”}]

Where the entire file is a single line and I have absolutely no control over this format.

I need to end up with a two dimensional Hash where the first dimension is keyed from the value in the ‘d’ field and the other fields are key/value pairs.

For example for the first record for this data:
my $one = 24
my $two = 2014-01-15
my $three = English
my $four = 3Y

The final hash would look like
my %Data
$Data{$four}{‘a’} = $one;
$Data{$four}{‘b’} = $two;
$Data{$four}{‘c’} = $three;

I know I can use “split (‘}’, $_);” to break the line into related sections. I could then further split this on “,” and “:” and take substrings to remove quotes and braces, and then fill everything in; but it seems like there should be an easier, more direct method, or maybe that is just wishful thinking on my part.

Can anyone suggest an easier method of reading a line of this format into a structure, which if not the final one that I am looking for, would at least allow me to drop some of the splits and/or substring commands it looks like I am going to need? Or just confirm that yes, I really do need to do this with numerous nested split commands.

Thanks in advance for any help.

RE: Trying to avoid nested split commands when parsing a file

These links look VERY helpful. Thank you.

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