Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Multiple lines in output! please some help!

Reply
Thread Tools

Multiple lines in output! please some help!

 
 
monte
Guest
Posts: n/a
 
      04-12-2005
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.

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-12-2005
"monte" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

> 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




--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      04-12-2005
monte <(E-Mail Removed)> wrote:

> 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.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-12-2005
monte <(E-Mail Removed)> wrote:

> 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.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Multiple Lines after a Specified Line -- Please Help! annie.promthana@gmail.com Perl Misc 2 05-04-2007 08:32 PM
Preserve blank lines when add multiple lines of text to a cell Cah Sableng Javascript 0 04-23-2007 04:46 AM
Asp.Net Calender, how to display 5 lines if there are only 5 lines in one month? Jack ASP .Net 9 10-12-2005 03:44 AM
multiple changes on multiple lines Dave Perl Misc 1 04-02-2005 05:06 PM
Modems, Analog Lines and ... Electrical Lines? Sens Fan Happy In Ohio Computer Support 5 09-02-2004 04:15 AM



Advertisments