rajkrishna89 has asked for the wisdom of the Perl Monks concerning the following question:

i have written a script which will extract the tags from a .doc file..

The format of the doc is

ID:001

Class:

Check1

Class:

Check2

ID:002

Class:

Check3

Class:

Check4

im getting the output in excel as below

Class ID

Check1 001

Check2 001

Check3 002

Check4 002

I want the output to be displayed as below:

Class ID

Check1 001

Check2

Check3 002

Check4

If there are multiple occurences (Classes) of same ID, the ID have to be written only once

The snippet is

my $i; for ($i = 0; $i <@array; $i++) { if($array[$i] =~ /^Count\:\s /) { $Count_Name = $1; for ($count = $i; $count >= 1; $count--) { if ($array[$count] =~ /ID:\s*(.+)/ ) { $ID = $1; my $Mycell1 = $Sheet->Range($Sheet->Cells($row +, $col),$Sheet->Cells($row, $col+2)); $Mycell1->{Value}=["Count_Name","$ID"]; $row++; # print $out_fh $sdd."\t".$Fun."\t".$File."\n" +; goto breakingfunction; } } breakingfunction: } } }

Replies are listed 'Best First'.
Re: Grouping Multiple Occurences
by umasuresh (Hermit) on Jan 09, 2012 at 13:40 UTC
Re: Grouping Multiple Occurences
by ansh batra (Friar) on Jan 09, 2012 at 15:24 UTC
    #! /usr/bin/perl open(FILE,"<pmregex.txt"); my @lines=<FILE>; close(FILE); print "Class ID\n"; my $found=0;my $id; foreach my $line(@lines) { if($line=~ /ID:/) { $id=$'; chomp($id); $found=0; } if($line=~ /Check./) { print "$&"; if($found==0) { print " $id";$found=1; } print "\n"; } }

    output
    Class ID Check1 001 Check2 Check3 002 Check4
    your code seems complex to me so i have written my own
    use flag if you want to print a id only once