Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Adding elements to an array from an array

by Rhodium (Scribe)
on May 01, 2002 at 17:45 UTC ( [id://163313]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks

I have a text file that I read in. I am trying to add words that match two different conditions to two different lists, and I am having some problems. I am hoping you all can look this over and tell me what I'm doing wrong.

open SCH, "< $runset{SchFile}" or die "Can't open $runset{SchFile}";{ local $/ = ".ENDS\n"; } while (<SCH>) { # Only look in our subckt of intrest! if ( m/^\.SUBCKT\s+($runset{SchCell})/ ){ print "\nFound $runset{SchCell}\n"; # For each word in the whole thing see if it matches the # two lists my ($word, %seen); $word = $_; foreach $word (@_) { print "$word"; if ($word =~ m/VDD|VCC/){ print "Matched VDD $word\n"; push @PN, $word if !$seen{Power}++; }elsif ($word =~ m/VSS|GND/){ print " Matched VSS $word\n"; push @GN, $word if !$seen{GND}++; } } } } close SCH; --DATA-- .SUBCKT FOO X y Z .PININFO test VDD VDD VDD Vss vdd VSS t y foo blah blah bla .ENDS .SUBCKT test2 vdds VSS VDD .PININFO test2 2FS SEL blah blah blah .ENDS --DATA--

Thanks so much for your help!


Rhodium

The <it>seeker</it> of perl wisdom.

Replies are listed 'Best First'.
Re: Adding elements to an array from an array
by thelenm (Vicar) on May 01, 2002 at 18:05 UTC
    The first thing I notice is that you match the text, but never use the results of your match. After the if statement, do something like this:
    if ( m/^\.SUBCKT\s+($runset{SchCell})/ ){ my $match = $1;
    Then $match will contain your matched string.

    After declaring $word and assigning $_ to it, you never use its contents. $word is overwritten on each iteration of the for loop.

    What is in @_, and why are you looping over it? Are you trying to get the words that matched earlier? If so, you should do something like this instead:

    foreach my $word (split ' ', $match) { if ($word =~ /^V(?:DD|CC)\z/) { # push @PN, etc. } elsif ($word =~ /^(?:VSS|GND)\z/) { # push @GN, etc. } }
    I hope is helpful in pointing you in the right direction.
Re: Adding elements to an array from an array
by perlplexer (Hermit) on May 01, 2002 at 18:19 UTC
    This is in addition to what thelenm already said.
    { local $/ = ".ENDS\n"; } while (<SCH>){ # ... }
    I see where you are going with this - good idea.
    However, the value of $/ will be changed back to "\n" right before the while() loop and you'll end up reading the file line-by-line.
    To fix, enclose the whole while loop in the same block as the "local $/;"
    { local $/ = ".ENDS\n"; while (<SCH>){ # ... } }

    --perlplexer
Re: Adding elements to an array from an array
by Mr. Muskrat (Canon) on May 01, 2002 at 18:15 UTC

    You're reading a "line" at a time into $_. Where line is everything up to '.END\n'. Then you overright $_ with the match in the regex. Then you assign it to $word. Then you attempt to loop through an undefined @_ assigning each value to $word. So your second and third regexes are never reached.

    Or did I miss something?


    Matthew Musgrove
    Who says that programmers can't work in the Marketing Department?
    Or is that who says that Marketing people can't program?
Re: Adding elements to an array from an array
by particle (Vicar) on May 01, 2002 at 18:44 UTC
    if i understand what you want, it's something like this:

    note: i'm using the DATA filehandle for this example, just plug in your handle to work on real data. i assume you're reading in a record delimited by ".ENDS\n", then splitting on space, and pushing to arrays depending on the match. you don't want to push more than once per record.

    correct me if i'm wrong.

    #!/usr/bin/perl -w use strict; $|++; my(@PN, @GN); $/ = ".ENDS\n"; my @a; while( <DATA> ) { my %seen; local $\ = "\n"; my @words = split / /, $_; for( @words ) { if( /VDD|VCC/ ) { print "Matched VDD $_\n"; push @PN, $_ unless $seen{Power}++; } elsif( /VSS|GND/ ) { print "Matched VSS $_\n"; push @GN, $_ unless $seen{GND}++; } } } print scalar @PN, "\n", scalar @GN, "\n"; __DATA__ .SUBCKT FOO X y Z .PININFO test VDD VDD VDD Vss vdd VSS t y foo blah blah bla .ENDS .SUBCKT test2 vdds VSS VDD .PININFO test2 2FS SEL blah blah blah .ENDS

    ~Particle *accelerates*

      Hi,

      Nice effort, but this grabs all of the records and the problem as most of you noted is that I am only trying to get the matches for a particular .SUBCKT. So for example I only want to get the /VDD|VCC/ and /VSS|GND/ for .SUBCKT FOO. And ignore .SUBCKT test2 all together..

      I hope this clears it up a bit. Thanks


      Rhodium

      The <it>seeker</it> of perl wisdom.

        Put the check back in.

        while( <DATA> ) { next unless m/^\.SUBCKT\s+$runset{SchCell}/; my %seen; . . .

        HTH,
        Charles K. Clarkson
        Clarkson Energy Homes, Inc.
Re: Adding elements to an array from an array
by mr.8 (Initiate) on May 01, 2002 at 20:38 UTC
    OK - im not exactly a newbie, nor an expert - just on the low rung on the ladder of knowledge here I guess. but... what exactly do: $|++; and $/=".END\n"; do?
    <bold>mr.8</bold>
      $|++ causes flushing to happen automatically on the currently selected output channel (this happens if $| is non-zero).

      $/=".END\n" changes the input record separator to the string ".END\n".

      See perlvar for more information.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://163313]
Front-paged by xtype
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 14:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found