Multiple lines in output! please some help!

Discussion in 'Perl Misc' started by monte, Apr 12, 2005.

  1. monte

    monte Guest

    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.
    monte, Apr 12, 2005
    #1
    1. Advertising

  2. "monte" <> wrote in
    news::

    > 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Apr 12, 2005
    #2
    1. Advertising

  3. monte <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 12, 2005
    #3
  4. monte <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 12, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    516
    Big Brian
    Oct 14, 2003
  2. Brian Smith
    Replies:
    0
    Views:
    353
    Brian Smith
    Feb 2, 2008
  3. Dave

    multiple changes on multiple lines

    Dave, Apr 2, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    94
    Gunnar Hjalmarsson
    Apr 2, 2005
  4. Replies:
    2
    Views:
    104
    Uri Guttman
    May 4, 2007
  5. Cah Sableng
    Replies:
    0
    Views:
    230
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page