LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Help with a perl script (http://www.linuxquestions.org/questions/programming-9/help-with-a-perl-script-517533/)

assasukasse 01-08-2007 06:14 PM

Help with a perl script
 
Hi everyone
i need help to fix this script for my new problem, the script was made by bigearsbilly, i tried only to edit it but failed, so i ask here what to do being unable to contact him.
this is the script
Code:

#!/usr/bin/perl -w

sub usage {

    die "Usage: $0 template-file coord-file\n";
}
usage() unless (scalar(@ARGV) == 2); # count params



($template, $data_file) = @ARGV; # get filenames


@ARGV = ($data_file);
@coords = <>;  # slurp coordinates
warn "Is '$data_file' a proper coordinate file? It contains non-numerics\n"
    if grep /[a-z]/i, @coords;

@ARGV = ($template);
@data = <>; # slurp template
die "No SDEF found! in $template" unless grep /SDEF/, @data;


foreach (@coords) {

    next unless /./; # jump blank lines

    ($nm, $x, $y, $z) = split;

    @copy = @data;

# do the sub for SDEF
    map {s/(SDEF pos=)[\d .]+(.*)/$1$x $y $z $2/} @copy;
    $filename = "$template.$nm";

    open OUT, ">$filename";
    print OUT @copy;
    close OUT;

}

it takes coordinates from a text file
then put them into another file on a particular position (looks after a SDEF pos= card)
saves the file with a filename according to the line number from where the coordinate was taken

what i need to change is this
about 5 or 6 lines before, in the file i need to edit, there is another card that needs to be edited with the same coordinates that go into the SDEF pos= place..

the line looks like:
Code:

14 RCC    6.45 0 10.134 0 0 1    0.55      $cella sorgente
where the cordinates to be edited are only the first 3 numbers
the spaces are mandatory, there are 5 spaces between RCC and the first number and five spaces between the last number and the 0.55 diameter.

I was thinking about adding this to the routine:
Code:

map {s/(14 RCC)[\d .]+(.*)/$1    $x $y $z $2/} @copy;
but seems that this simple solution doesn't work..i get output files that have not been changed at all, just renamed..
what should i do?
thank you for your help

assasukasse 01-10-2007 03:30 AM

i think there are two problems:
one is the syntax i should use to run the two find and change operations
Code:

map (----) @copy;
map (////) @copy;

simply doesn't work

second is the regex expression linked to the additional line i need to edit:
Code:

map {s/(14 RCC)[\d .]+(.*)/$1$x $y $z $2/} @copy;
this one puts the coordinates too near the RCC, i need 4 or 5 spaces between RCC and the coords,
moreover it doesn't retain the coords that follows, so in the end i get something like this:
Code:

14 RCC6.45 0 10.134    $cella sorgente
where all the other needed coords are lost

what is the correct regex? i spent 3 hours yesterday reading tutorials and guides but still i couldn't make it work (or better, now it does do something at least after i added the additional routine, but is still too far from the perfect working order of when bigearsbilly gave me)
Thank you for your help

assasukasse 01-11-2007 04:34 AM

this morning i had an hint from a friend and
i edited this way:

Code:

map {s/(14 RCC *)[^ ]+ [^ ]+ [^ ]+/$1$x $y $z/} @copy;
map {s/(SDEF *pos=)[^ ]+ [^ ]+ [^ ]+/$1$x $y $z/} @copy;

at this moment (WINZOZE pc at work) i can't try it
but could it work?
thanks


All times are GMT -5. The time now is 01:05 PM.