Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Parsing question....

Status
Not open for further replies.

jping45

Technical User
Aug 22, 2001
49
0
0
US
I have a file:

Class1:
entry1 = <some text>
entry2 = <some text>
entry3 = <some text>

Class2:
entry1 = <some text>
entry2 = <some text>
entry3 = <some text>

Class3:
entry1 = <some text>
entry2 = <some text>
entry3 = <some text>
.
.
.

Here's my problem... I need to search the file for the Class1 paragraph then grab the <some text> from entry1 and <some text> from entry2 and assign the text to two variables which I use to create a later on in the perl program I'm trying to write. Now I can search the file for the Class1 paragraph, however I have not been able to figure out how to assign the variables... any suggestions would be greatly appreciated.
regards,
jping45
 
If you're using regexes, then you'll probably want to use $&. $& is the captured text from the most recent regex. Maybe something like this ...
Code:
my $str = "class1:\n	entry1: bumble.\"";
if ($str =~ /entry1:(\s+|\t+)[a-z0-9_\.,:;\"]*/i) {
  $str = $&;
}

 
Actually the requirments have changed... now I need to search the file for a variable (which can be class1 or class2 or class3, so on... ) and print the paragraph to a file that will be used for report generation...
 
Hi jping,

Here's an example of how to parse your file into a data structure. It might not be exactly what you need, but it will certainly give you some ideas hopefully.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Data::Dumper[/green][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%data[/blue] = [red]([/red][red])[/red][red];[/red]

[black][b]my[/b][/black] [blue]$section[/blue] = [red]'[/red][purple][/purple][red]'[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<DATA>[red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
	[olive][b]next[/b][/olive] [olive][b]if[/b][/olive] [red]/[/red][purple]^$[/purple][red]/[/red][red];[/red]

	[olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]^([purple][b]\w[/b][/purple]+):[/purple][red]/[/red][red])[/red] [red]{[/red]
		[blue]$section[/blue] = [blue]$1[/blue][red];[/red]
		[url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Duplicate section found: [blue]$section[/blue][/purple][red]"[/red] [olive][b]if[/b][/olive] [url=http://perldoc.perl.org/functions/exists.html][black][b]exists[/b][/black][/url] [blue]$data[/blue][red]{[/red][blue]$section[/blue][red]}[/red][red];[/red]
		[blue]$data[/blue][red]{[/red][blue]$section[/blue][red]}[/red] = [red]{[/red][red]}[/red][red];[/red]
		
	[red]}[/red] [olive][b]elsif[/b][/olive] [red]([/red]! [blue]$section[/blue][red])[/red] [red]{[/red]
		[black][b]die[/b][/black] [red]"[/red][purple]Data found before section: [blue]$_[/blue][/purple][red]"[/red][red];[/red]
		
	[red]}[/red] [olive][b]elsif[/b][/olive] [red]([/red][red]/[/red][purple]^[purple][b]\s[/b][/purple]*([purple][b]\w[/b][/purple]+)[purple][b]\s[/b][/purple]*=[purple][b]\s[/b][/purple]*(.*)[/purple][red]/[/red][red])[/red] [red]{[/red]
		[black][b]die[/b][/black] [red]"[/red][purple]Duplicate entry for section found: [blue]$section[/blue], [blue]$1[/blue], [blue]$data[/blue]{[blue]$section[/blue]}{[blue]$1[/blue]} - [blue]$2[/blue][/purple][red]"[/red]
			[olive][b]if[/b][/olive] [black][b]exists[/b][/black] [blue]$data[/blue][red]{[/red][blue]$section[/blue][red]}[/red][red]{[/red][blue]$1[/blue][red]}[/red][red];[/red]
		[blue]$data[/blue][red]{[/red][blue]$section[/blue][red]}[/red][red]{[/red][blue]$1[/blue][red]}[/red] = [blue]$2[/blue][red];[/red]
		
	[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
		[black][b]die[/b][/black] [red]"[/red][purple]Unrecognized data found: [blue]$_[/blue][/purple][red]"[/red][red];[/red]
	[red]}[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [maroon]Dumper[/maroon][red]([/red]\[blue]%data[/blue][red])[/red][red];[/red]

[teal]__DATA__[/teal]
[teal]Class1:[/teal]
[teal]     entry1 = <some text>[/teal]
[teal]     entry2 = <some text>[/teal]
[teal]     entry3 = <some text>[/teal]

[teal]Class2:[/teal]
[teal]     entry1 = <some text>[/teal]
[teal]     entry2 = <some text>[/teal]
[teal]     entry3 = <some text>[/teal]

[teal]Class3:[/teal]
[teal]     entry1 = <some text>[/teal]
[teal]     entry2 = <some text>[/teal]
[teal]     entry3 = <some text>[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]Data::Dumper - stringified perl data structures, suitable for both printing and eval[/li]
[/ul]
[/tt]

Output:
Code:
[blue]$VAR1[/blue] = [red]{[/red]
          [red]'[/red][purple]Class3[/purple][red]'[/red] => [red]{[/red]
                        [red]'[/red][purple]entry2[/purple][red]'[/red] => [red]'[/red][purple]<some text>[/purple][red]'[/red],
                        [red]'[/red][purple]entry3[/purple][red]'[/red] => [red]'[/red][purple]<some text>[/purple][red]'[/red],
                        [red]'[/red][purple]entry1[/purple][red]'[/red] => [red]'[/red][purple]<some text>[/purple][red]'[/red]
                      [red]}[/red],
          [red]'[/red][purple]Class1[/purple][red]'[/red] => [red]{[/red]
                        [red]'[/red][purple]entry2[/purple][red]'[/red] => [red]'[/red][purple]<some text>[/purple][red]'[/red],
                        [red]'[/red][purple]entry3[/purple][red]'[/red] => [red]'[/red][purple]<some text>[/purple][red]'[/red],
                        [red]'[/red][purple]entry1[/purple][red]'[/red] => [red]'[/red][purple]<some text>[/purple][red]'[/red]
                      [red]}[/red],
          [red]'[/red][purple]Class2[/purple][red]'[/red] => [red]{[/red]
                        [red]'[/red][purple]entry2[/purple][red]'[/red] => [red]'[/red][purple]<some text>[/purple][red]'[/red],
                        [red]'[/red][purple]entry3[/purple][red]'[/red] => [red]'[/red][purple]<some text>[/purple][red]'[/red],
                        [red]'[/red][purple]entry1[/purple][red]'[/red] => [red]'[/red][purple]<some text>[/purple][red]'[/red]
                      [red]}[/red]
        [red]}[/red][red];[/red]

- Miller
 
If you don't need to modify/manipulate the data, this might work for you:
Code:
my $target = 'Class2';
local $/ = "\n\n";

while (<DATA>) {
    chomp;
    print $_ if /$target/;
}

__DATA__
Class1:
     entry1 = <some text>
     entry2 = <some text>
     entry3 = <some text>

Class2:
     entry1 = <some text>
     entry2 = <some text>
     entry3 = <some text>

Class3:
     entry1 = <some text>
     entry2 = <some text>
     entry3 = <some text>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top