Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-08-2007, 06:14 PM   #1
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 132

Rep: Reputation: 15
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
#!/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:
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:
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
Old 01-10-2007, 03:30 AM   #2
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 132

Original Poster
Rep: Reputation: 15
i think there are two problems:
one is the syntax i should use to run the two find and change operations
map (----) @copy;
map (////) @copy;
simply doesn't work

second is the regex expression linked to the additional line i need to edit:
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:
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
Old 01-11-2007, 04:34 AM   #3
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 132

Original Poster
Rep: Reputation: 15
this morning i had an hint from a friend and
i edited this way:

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?


expression, help, perl, regex, script

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to run a shell script in the perl script sharad Linux - General 1 05-24-2006 03:23 AM
perl script crewblunts Programming 3 03-08-2006 02:25 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:27 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration