in reply to Re^2: Parsing a file and finding the dependencies in it
in thread Parsing a file and finding the dependencies in it
In this special situation, you can just test for /^Desc:/. The technique is to limit the number of things returned from the split, in this case 2 things. Doing that requires that we take care of one more detail, a chomp() is needed.
When we let split() do its default thing, a chomp() is not needed because the trailing \n will be removed (default split is on any sequence of the 5 whitespace characters (space,\n,\f,\r,\t). If we tell split() to stop working after it has 2 things, then we have to do manually what it would have done to the last thing.
I set up %record so that it is a Hash of Array, each value is a reference to an anonymous array of data. That is true even for a single value like the id number. "Same-ness" is a good thing in programming. So, I would do the same for the description string.
Then the question of so what do you do with this description once the record is complete? You could say put another dimension on the hash which has id's as the key. However, there is something to be said for keeping things simple. You could just make another hash that is keyed on id's with the string as the value. Some purists might shudder in horror, but again simplicity has virtues!
# ........ snip if (my $num = /\[/.../\]/) { if (/^Desc:/) { chomp; my ($desc, $string) = split(/\s+/,$_,2); $record{$desc} = [$string]; next; } my ($tag, @values) = split; @{$record{$tag}} = @values; #........ snip OR....perhaps... if (/^Desc:/) { chomp; my ($desc, $string) = split(/\s+/,$_,2); $record{$desc} = [$string]; # same as @{$record{$desc}} = ($s +tring); } else { my ($tag, @values) = split; @{$record{$tag}} = @values; #....snip...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Parsing a file and finding the dependencies in it
by legendx (Acolyte) on Jul 07, 2011 at 13:28 UTC | |
|
Re^4: Parsing a file and finding the dependencies in it
by legendx (Acolyte) on Jul 07, 2011 at 14:49 UTC | |
by Marshall (Canon) on Jul 09, 2011 at 15:10 UTC | |
by Anonymous Monk on Jul 09, 2011 at 15:23 UTC |