On Feb 3, Steven M. Klass said:

># First we use the tool ./fh to go create the file hierachy file. This will 
># return something that looks like the following:

>#       possibly the top cell
>#           topcell : 1
>#               subcell0 : 2
>#               subcell1 : 2
>#                   subsubcell0 :3
>#               subcell0 : 2
>#               subcell2 : 2
>#                  subcell0 : 3
>#                  subcell1 : 3
>#                       subsubcell0 : 4

># Now all I want to do is parse this file for individually created cells.
># 1. So, I need to remove the first line - it's always garbage.
># 2. I also need to remove any cells that contain "$"  as they too aregarbage.
># 3. I also want to strip the ":" and anything that follows
># 4. Lastly, I don't want duplicates so I need to check them to make sure 
># the names (subcells) don't already exist.

>while (<CELL>) {
>      my $line  = $_;
>      chop($line);
>      for ($line) {
>        # Remove the first line -->>CHEESY<<-- (Solves #1) 
>        s/possibly.*/\ /;
>        # Remove all Cadence PCells (Solves #2)
>        s/\b.*\$.*//;
>        # Remove every level of hier (Solves #3)
>        s/\ :.*\n//; 
>        print LIST "$list"; 
>      } 
>}

I don't see why you're reading a line into $_, copying it to $file, and
then looping over $file to get $_ again.  And where'd $list come from?!

  my (@cells, %seen);
  while (<CELL>) {
    next if $. == 1 or /\$/;  # skip first line and lines with $
    s/\s+:.*//s;  # remove everything after the colon
    push @cells, $_ if !$seen{$_}++;
  }

If you don't understand that last line, let me expand it for you:

  if (!$seen{$_}++) {
    push @cells, $_;
  }

This uses the magic of post-increment.  The first time a cell-name is
seen, $seen{"..."} will be 0.  $seen{$_}++ returns the value of $seen{$_}
and then increments the original -- so we get 0 back, but afterwards, the
value is 1.  Here's a quick demo:

  $a = 10;
  $b = $a++;
  print "$b $a";  # 10 11

  # compare with

  $a = 10;
  $b = ++$a;
  print "$b $a";  # 11 11

The ! in front of $seen{$_}++ takes the logical opposite of the value
returned.  If $seen{$_} returns 0, that means we've never seen the value
before, so !$seen{$_} returns 1, so the if statement is executed, and the
cell name is push()ed to the @cells array.  AS A SIDE EFFECT, $seen{$_} is
incremented one.

If $seen{$_} returns a non-zero value, that means we've seen it before, so
it's a duplicate, so we don't need to keep it.  AS A SIDE EFFECT,
$seen{$_} is incremented one.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to