Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Simple replacement - only if they exist..

by Rhodium (Scribe)
on Apr 11, 2002 at 17:18 UTC ( #158367=perlquestion: print w/ replies, xml ) Need Help??
Rhodium has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Yesterday I think I asked a a bit more than what I should have. Since then I have broken down the problem considerably, and here is where I'm at.
I have some variables that I need to modify then replace. They are

$runset{"POWER-NODE"}="vdd,vdda"; $runset{"GROUND-NODE"}="vssa,vss";
Now I have a line in a text file that I want to modify
$line = "*.PIN foo:I bar:O vdd:I vssq:I vssa:O";
The rules are simple

  • For any variables in $runset{"POWER-NODE"} that match the core word in $line will become "variable:P" so in this case $runset{"POWER-NODE"}
    vdd becomes vdd:P and vdda becomes vdda:P in $line
  • This would produce..
    $line = "*.PIN foo:I bar:O vdd:P vssq:I vssa:O";
  • For any variables in $runset{"GROUND-NODE"} that match the core word in $linewill become "variable:G" so in this case $runset{"GROUND-NODE"} vssa becomes vssa:G and vss becomes vss:G in $line
  • This would produce..continuing from previous
    $line = "*.PIN foo:I bar:O vdd:P vssq:I vssa:G";
    Notice that only those variables that exist in both $line and $runset{pwr/gnd} were changed. Also I don't want to actually change the $runset variables only temporarily modify them.

    I hope this is simpler to understand. I think this isn't tough but I am really stuck on this thing..
    Any help would be very much appreciated. Thanks to every one so far that has tried to read through my kludge and help me out

    Comment on Simple replacement - only if they exist..
    Select or Download Code
    Re: Simple replacement - only if they exist..
    by sfink (Deacon) on Apr 11, 2002 at 17:38 UTC
      Assuming I understand the problem correctly, you just need to convert your hashtable values to regexp patterns that match any of the variables, then do a substitution. Something like:
      sub replace { my ($line, $nodes, $suffix) = @_; (my $matchNodes = $nodes) =~ s/,/|/g; $line =~ s/\b($matchNodes):./$1:$suffix/g; return $line; } $line = replace($line, $runset{'POWER-NODE'}, 'P'); $line = replace($line, $runset{'GROUND-NODE'}, 'G');

      That assumes that there's always a single character after the colon.

      Notice the \b -- that prevents it from replacing eg 'xvdd' when your runset contains 'vdd'.

    Re: Simple replacement - only if they exist..
    by PrakashK (Pilgrim) on Apr 11, 2002 at 17:46 UTC
      I think it would be helpful, if you slightly change your data structure, or add a new one:
      %rs = ( vdd => 'P', vdda => 'P', vssa => 'G', vss => 'G', );
      Then use this table to modify your input $line.
      $line = "*.PIN foo:I bar:O vdd:I vssq:I vssa:O"; $line =~ s/\b(\w+):([A-Z])\b/$rs{$1} ? "$1:$rs{$1}" : "$1:$2"/eg;
      /prakash
    Re: Simple replacement - only if they exist..
    by erikharrison (Deacon) on Apr 11, 2002 at 17:58 UTC

      Okay. . . .I've looked through your previous nodes on this and here (as far as I understand it) is your solution, in pieces.

      What you have is a hash with a comma delimited set of values. I'm going to assume you are stuck with this structure. In the *.PIN line, a variable (as you call it) seems to be in the whitepace, name, colon, value format. You want to change the letter appended after the colon on anything inside these comma seperated list, and make this change based on which hash entry these are in. We'll use a regular expression for this.

      #Assuming that $variable contains the value to match for #( Something from $runset{GROUND-NODE} ) . . . $line =~ s/ #The substitution operator $variable #vssa, vss, etc . . . : #a colon . #any character (I, O, etc) / #substituted by . . . $variable #as before : #as before G #the G because it's in GROUND-NODE /xg; #Substitute globally

      Make sure you understand regexes! If you don't then a simple one will throw you, and that is the solution you need here.

      So, now that we has the regex, we just do a couple of iterations, and you are set.

      #Complete code! #(Almost . . .) # @lines contains the *PIN lines . . . foreach $PIN (@lines) { foreach $ground_var (split /,/, $runset{GROUND-NODE}) { $PIN =~ s/$ground_var:./$ground_var:G/g; } foreach $power_var (split /,/, $runset{POWER-NODE}) { $PIN =~ s/$power_var:./$power_var:P/g }

      Now, if you need explanations, ask and I'll give 'em :-).

      Cheers,
      Erik

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: perlquestion [id://158367]
    Approved by mdillon
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (6)
    As of 2014-10-31 07:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (215 votes), past polls