E
el.dodgero
I'm having a brainfart day, so I'll just throw this up to see if
there's some easy solution already out there that I don't know about...
I have data in a format that looks like it should be easy to parse, but
it's not acting that way...
For the most part, most of the data is in a simple sort of structure,
like so:
something whatever:2
{
property blah
otherproperty blahblah
}
However some of the time it's more like this:
something foo:3
{
property blah
otherthings
{
otherthingType1 identifier
{
property1 1
property2 2
moreInfoThanYouNeed
{
data 1 yadda
data 2 yaddayadda
data 3 yaddayaddayadda
}
}
otherthingType2 identifier
{
property1 0
property2 foo
}
}
The trick is that the whole {} block of anything is one biug property
of the stuff above it. The nesting could go on and on forever in theory
though normally sits at about 0-5 deep
I'm wanting to parse this stuff into a data structure. The thing is, if
I go line by line, then by the time I'm at a { line to know I'm in a
subnested block, I've already passed by the thing I have to set it as a
property of. Thus I'm thinking two things:
1) The reading-in of any given {} block should be a subroutine that
knows to call itself. This makes it convenient that all these files
start and end with a { and } respectively, putting the whole thing
inside a big block like this.
2) I should keep a reference to the last thingy I set, the previous
token, so to speak.
3) I should make a simple little subroutine called 'assign' that
simplifies the process of assigning a property to a token that may
already have it -- since I don't want to redefine such property of a
token, I should assign or append to an array. Since I don't know what
will need an array ahead of time for sure, a subroutine that handles
the assigning would be good, something like this:
sub assign ($$) {
my $thing = shift;
my $newval = shift;
if (defined $thing) {
unless (ref $thing eq 'ARRAY') {
$thing = [$thing];
}
push @{$thing}, $newval;
}
else {
$thing = $newval;
}
}
But anything i'm not thinking of... well, I can't exactly ask 'Am I not
thinking of this?' if I have no clue that 'this' may be. So this is
sort of that whole 'pointing and question marks' thing....
BTW if no one answers this, it's okay.
This is sort of therapeutic to write it all out like this too.
Who knows, maybe someone else has a similar scenario where my thoughts
will help them. *shrug*
BTW -- the vasy majority will either pass this by without a second
thought, suggest things to me that will help, or say that something I
said helped them with their problem. The first don't bother me, the
second I thank (and will, not just in advance), and the third, no
worries.
However, there is someone out there who's wanting to answer this in
some asshole way just to get some smartass comment in for no good
reason than their own narcissistic joy at seeing the oh-so-clever thing
they typed. I know this deep in my soul because I know this is usenet.
So, if you are that person, just get a life instead, ok?
there's some easy solution already out there that I don't know about...
I have data in a format that looks like it should be easy to parse, but
it's not acting that way...
For the most part, most of the data is in a simple sort of structure,
like so:
something whatever:2
{
property blah
otherproperty blahblah
}
However some of the time it's more like this:
something foo:3
{
property blah
otherthings
{
otherthingType1 identifier
{
property1 1
property2 2
moreInfoThanYouNeed
{
data 1 yadda
data 2 yaddayadda
data 3 yaddayaddayadda
}
}
otherthingType2 identifier
{
property1 0
property2 foo
}
}
The trick is that the whole {} block of anything is one biug property
of the stuff above it. The nesting could go on and on forever in theory
though normally sits at about 0-5 deep
I'm wanting to parse this stuff into a data structure. The thing is, if
I go line by line, then by the time I'm at a { line to know I'm in a
subnested block, I've already passed by the thing I have to set it as a
property of. Thus I'm thinking two things:
1) The reading-in of any given {} block should be a subroutine that
knows to call itself. This makes it convenient that all these files
start and end with a { and } respectively, putting the whole thing
inside a big block like this.
2) I should keep a reference to the last thingy I set, the previous
token, so to speak.
3) I should make a simple little subroutine called 'assign' that
simplifies the process of assigning a property to a token that may
already have it -- since I don't want to redefine such property of a
token, I should assign or append to an array. Since I don't know what
will need an array ahead of time for sure, a subroutine that handles
the assigning would be good, something like this:
sub assign ($$) {
my $thing = shift;
my $newval = shift;
if (defined $thing) {
unless (ref $thing eq 'ARRAY') {
$thing = [$thing];
}
push @{$thing}, $newval;
}
else {
$thing = $newval;
}
}
But anything i'm not thinking of... well, I can't exactly ask 'Am I not
thinking of this?' if I have no clue that 'this' may be. So this is
sort of that whole 'pointing and question marks' thing....
BTW if no one answers this, it's okay.
This is sort of therapeutic to write it all out like this too.
Who knows, maybe someone else has a similar scenario where my thoughts
will help them. *shrug*
BTW -- the vasy majority will either pass this by without a second
thought, suggest things to me that will help, or say that something I
said helped them with their problem. The first don't bother me, the
second I thank (and will, not just in advance), and the third, no
worries.
However, there is someone out there who's wanting to answer this in
some asshole way just to get some smartass comment in for no good
reason than their own narcissistic joy at seeing the oh-so-clever thing
they typed. I know this deep in my soul because I know this is usenet.
So, if you are that person, just get a life instead, ok?