Multiple lines in output! please some help!

M

monte

I am a new user and I am parsing and file that contains a heirarchy. i
would like to get the cell name once and the libraries that that the
cell was found. my code is repeating he output everytime it goes into
and produces multiple listing of the cell when I only like to have this
done once. Can anyone help me figure this out?
1)suppose the input file is formatted like this
1. libA cellB
2. libC cellD
....
8. libF cellG
9. libX cellB

2) desire output:
CELL:cellB LIB:libA libX
CELL:cellD LIB: libC
CELL:cellG LIB: libF

4) my code:
while(<>)
{
chomp;
$show_file=$_;
next if ( $show_file=~ /\#/ );
#print $show_file;
$show_file=~/^\s+\d\.\s+(\S+)\s+(\S+)/;
$lib=$1;
$cell=$2;

#checking if the same cell exist in the same heirarchy
($rec{$2}= "Cell:$2 Lib:") if (! exists($rec{$2}));
$rec{$2}.="$1\t";
foreach $entry(keys %rec)
{
print" $rec{$entry}\n";
}}#last braket for 1st foreach loop

5) My output list the cells and libs everytime it goes into the loop
and I only like to have the cell listed once with the libraries
associated with it.
 
A

A. Sinan Unur

I am a new user and I am parsing and file that contains a heirarchy. i
would like to get the cell name once and the libraries that that the
cell was found. my code is repeating he output everytime it goes into
and produces multiple listing of the cell when I only like to have
this done once. Can anyone help me figure this out?
1)suppose the input file is formatted like this
1. libA cellB
2. libC cellD
...
8. libF cellG
9. libX cellB


Please do read the posting guidelines to find out how you can post an
accurate depiction of the data your script is using in a ready-to-run
format.

If I wanted to edit/rewrite your script, I have to do extra work just
getting the data formatted so that I can run it.

The guidelines provide information on how you can help others help you.
2) desire output:
CELL:cellB LIB:libA libX
CELL:cellD LIB: libC
CELL:cellG LIB: libF

4) my code:

Please post code that is ready to be run, and indent it properly.

use strict;
use warnings;
while(<>)
{
chomp;
$show_file=$_;
next if ( $show_file=~ /\#/ );
#print $show_file;
$show_file=~/^\s+\d\.\s+(\S+)\s+(\S+)/;
$lib=$1;
$cell=$2;

You should always check if the match actually succeeded before using $1
and $2 here.
#checking if the same cell exist in the same heirarchy
($rec{$2}= "Cell:$2 Lib:") if (! exists($rec{$2}));
$rec{$2}.="$1\t";
foreach $entry(keys %rec)
{
print" $rec{$entry}\n";
}}#last braket for 1st foreach loop

Such comments would not be needed if you indented your code properly.

Here is one way to do it:

use strict;
use warnings;

my %cells;

while(<DATA>) {
chomp and length or last;
if(/^\d+\.\s+(\w+)\s+(\w+)/) {
push @{ $cells{$2} }, $1;
}
}

for my $cell (keys %cells) {
print "CELL: $cell LIB: @{ $cells{$cell} }\n";
}

__END__
1. libA cellB
2. libC cellD
....
8. libF cellG
9. libX cellB
 
T

Tad McClellan

monte said:
4) my code:
while(<>)
{
chomp;
$show_file=$_;
next if ( $show_file=~ /\#/ );
#print $show_file;
$show_file=~/^\s+\d\.\s+(\S+)\s+(\S+)/;
$lib=$1;
$cell=$2;

#checking if the same cell exist in the same heirarchy
($rec{$2}= "Cell:$2 Lib:") if (! exists($rec{$2}));
$rec{$2}.="$1\t";
foreach $entry(keys %rec)
{
print" $rec{$entry}\n";
}}#last braket for 1st foreach loop


Something horrid has happened to the formatting of your code.

You'd better figure out how to fix that if you expect volunteers
to read it.
 
T

Tad McClellan

monte said:
while(<>)
{
chomp;
$show_file=$_;


You can replace all of that with just:

while ( $show_file = <> )
{
chomp $show_file;


Whitespace is not a scarce resource, feel free to use as much of
it as you like to make your code easier to read.

next if ( $show_file=~ /\#/ );


You do not neet to backslash the #, it is not special.

$show_file=~/^\s+\d\.\s+(\S+)\s+(\S+)/;
$lib=$1;
$cell=$2;


You should never use the dollar-digit variables unless you have
first ensured that the pattern match *succeeded*:

if ( $show_file =~ /^\s+\d\.\s+(\S+)\s+(\S+)/ ) {
$lib=$1;
...

}}#last braket for 1st foreach loop


If you indented properly, then you wouldn't have to make
those little notes to yourself.

5) My output list the cells and libs everytime it goes into the loop
and I only like to have the cell listed once with the libraries
associated with it.


I am not going to try and analyse your code, in an effort to help
you with your problem.

It is too ugly to look upon.

Fix it and post again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top