Wow, both examples from both posters were great!
Paul, thanks for the explanation!! I'll start playing :) I suppose the fileIO 
part will be easy? I am just at perldoc right now!

Jlc


-----Original Message-----
From: Paul Lalli [mailto:[EMAIL PROTECTED]
Sent: Wednesday, July 11, 2007 12:30 PM
To: beginners@perl.org
Subject: Re: Search and Replace

On Jul 11, 1:50 pm, [EMAIL PROTECTED] (Joseph L. Casale)
wrote:
> Hi,
> Know that I am learning perl, I am expected to use it at work :)
> Problem is I am still to green for the current problem I have. The data is 
> always left justified and has a space between each value.
>
> I have a text file of about ~500 lines like this:
> -11.67326 23.95923 0.4617566
> 5.075023 24.27938 0.4484084
> 6.722163 -24.68986 1.399011
> -11.2023 -25.0398 1.145933
>
> I need to do the following:
> Insert and X, Y and Z as one script:
> X-11.67326 Y23.95923 Z0.4617566
> X5.075023 Y24.27938 Z0.4484084
> X6.722163 Y-24.68986 Z1.399011
> X-11.2023 Y-25.0398 Z1.145933
>
> Lastly, I'll need to make an additional copy of the program to strip out any 
> numerical values for Z, and replace them with the following, [some_var].
> X-11.67326 Y23.95923 Z[some_var]
> X5.075023 Y24.27938 Z[some_var]
> X6.722163 Y-24.68986 Z[some_var]
> X-11.2023 Y-25.0398 Z[some_var]
>
> Any help would be appreciated greatly, I am still just to new for this!

#!/usr/bin/env perl
use strict;
use warnings;

my @data = <DATA>;
#Part A
for my $line (@data){
  $line =~ s/(\S+)\s+(\S+)\s+(\S+)/X$1 Y$2 Z$3/;
  print $line;
}

print "\n\n";
#Part B
for my $line (@data) {
  $line =~ s/(?<=Z)\d+(\.\d+)?/[some var]/;
  print $line;
}


__DATA__
-11.67326 23.95923 0.4617566
5.075023 24.27938 0.4484084
6.722163 -24.68986 1.399011
-11.2023 -25.0398 1.145933


Output:
X-11.67326 Y23.95923 Z0.4617566
X5.075023 Y24.27938 Z0.4484084
X6.722163 Y-24.68986 Z1.399011
X-11.2023 Y-25.0398 Z1.145933


X-11.67326 Y23.95923 Z[some var]
X5.075023 Y24.27938 Z[some var]
X6.722163 Y-24.68986 Z[some var]
X-11.2023 Y-25.0398 Z[some var]

In the first, we simply search for the three strings of non-whitespace
and capture them in $1, $2, and $3, then we replace the whole string
with X followed by $1, Y followed by $2, and Z followed by $3

In the second, we search for any sequence of numbers (possibly
followed by a period and more numbers) that was preceeded by a Z, and
replace that sequence with [some var].

For more information on regular expressions, please see:
perldoc perlre
perldoc perlretut
perldoc perlreref

Paul Lalli


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/







--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to