LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-31-2010, 07:24 PM   #46
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723

I can separate x and y perfectly, I just can't separate the green and red parts. I haven't thought of using Text::Balanced for this, I will try and report back.
 
Old 01-31-2010, 07:27 PM   #47
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
I can separate x and y perfectly, I just can't separate the green and red parts. I haven't thought of using Text::Balanced for this, I will try and report back.
???????????????????????????????????????????????????????????????

Then why/what for have we been discussing Text::Balanced in the first place ?

And, of course, just one pair (no nesting) of any type of brackets can be parsed by regular expression, but don't reinvent the wheel - Text::Balanced does this already.
 
Old 01-31-2010, 07:33 PM   #48
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
This seems to work:

Code:
sub process_pstruct() {
	my @parts = extract_bracketed($_[0], '{}', '[^{]*');
	
	$parts[0] =~ s/{([^}]*)}/\1/;
	my @members = split(/\s*;\s*/, $parts[0]);
	
	my @parents = split(/\s+/, $parts[2]);
	shift(@parents);
	my $name = shift(@parents);
	
	print "#define $name\_PSTRUCT_TEMPLATE ";
	for $parent (@parents) {
		print "$parent\_PSTRUCT_TEMPLATE "
	}
	print "\\\n";
	for $member (@members) {
		if($member !~ /^\s*$/) {
			$member =~ s/\s*([^\s]*)\s*/\1/;
			print "\t$member;\\\n";
		}
	}
	print "\n";
	
	print "struct $name {\n";
	print "\t$name\_PSTRUCT_TEMPLATE\n";
	print "}\n";
	print "typedef struct $name $name;\n";
}
 
Old 01-31-2010, 07:35 PM   #49
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Sergei Steshenko View Post
???????????????????????????????????????????????????????????????

Then why/what for have we been discussing Text::Balanced in the first place ?

And, of course, just one pair (no nesting) of any type of brackets can be parsed by regular expression, but don't reinvent the wheel - Text::Balanced does this already.
This is a different situation here then the one I was discussing earlier. Here I want to split the string at the first left bracket and don't care about nesting, before I was talking about finding the location of the corresponding right bracket.

And I still will need to do that to find the text to feed into process_pstruct().

And then how do I replace the original text with the one retured by process_pstruct()?
 
Old 01-31-2010, 09:05 PM   #50
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
This is a different situation here then the one I was discussing earlier. Here I want to split the string at the first left bracket and don't care about nesting, before I was talking about finding the location of the corresponding right bracket.

And I still will need to do that to find the text to feed into process_pstruct().

And then how do I replace the original text with the one retured by process_pstruct()?
A general answer to the item in bold:
  1. if you use regular expressions, there are predefined Perl variables which hold the part before match, the matching part and, I think, the remainder, but remainder can always be constructed from the whole test, part before match, and match; anyway, see below on 'length' and 'substr';
  2. if you are using Text::Balanced, you definitely have $prefix, $extracted, $remainder - conceptually the same as above;
  3. 'length' (perldoc -f length) returns string length;
  4. 'substr' (perldoc -f substr) can be used as LHS of assignment, so any part of string can be replaced by something else.
 
Old 02-01-2010, 07:16 AM   #51
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Are you saying that modifying extract_bracketed()'s output will modify the original string?

And I already made process_pstruct() return a string instead of printing to stdout.
 
Old 02-01-2010, 08:18 AM   #52
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
Are you saying that modifying extract_bracketed()'s output will modify the original string?

...

No, I'm not saying that.

I'm saying that having the original text and extracted part one can then replace the extracted part with something else in original text.
 
Old 02-01-2010, 08:43 AM   #53
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Don't you need to know the index, too?

A regex that I think would match a pstruct definition would be /pstruct[^}]+}/.
 
Old 02-01-2010, 01:15 PM   #54
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
Don't you need to know the index, too?

A regex that I think would match a pstruct definition would be /pstruct[^}]+}/.
As I said, 'length' can be used, and it returns in this case index - if length of prefix is taken.
 
Old 02-01-2010, 01:16 PM   #55
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
...
A regex that I think would match a pstruct definition would be /pstruct[^}]+}/.
Not quite - think of
Code:
FooBarpstruct { ... }
case.
 
Old 02-01-2010, 01:20 PM   #56
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Sergei Steshenko View Post
Not quite - think of
Code:
FooBarpstruct { ... }
case.
And, actually, think also of

Code:
FooBarpstructDooDah { ... }
case.
 
Old 02-01-2010, 02:07 PM   #57
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
How would you fix that?
 
Old 02-01-2010, 02:45 PM   #58
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
How would you fix that?
You know my recipe - read. And here is clue for you: word boundary.

perldoc perlretut
perldoc perlre

are your friends; you need less than 350 lines of patience.
 
Old 02-01-2010, 04:00 PM   #59
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Good, but how do iterate over all the instances of pstruct { ... } in the file using regex matching?
 
Old 02-01-2010, 04:20 PM   #60
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I figured that out, but now how do I replace the match with the function's return?
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
checking for XML::Parser... configure: error: XML::Parser perl module is required for kornerr Linux - General 11 11-16-2008 07:24 AM
perl xml::parser dirhandle problem theshark Linux - Software 0 03-16-2006 06:45 PM
XML::Parser perl module is required for intltool, for LogJam GT_Onizuka Linux - Newbie 7 06-30-2005 07:49 AM
XML::Parser perl module is required farzan Linux - Software 8 09-26-2004 05:54 AM
XML::Parser perl mod is req 4 intltool error BorisMcHack Slackware 4 06-23-2004 07:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration