Re: How to put a variable value into a text file

2004-01-30 Thread stuart_clemons
Hi Joseph:

Thanks very much for the feedback.  Unfortunately for me, I've been 
totally swamped with tasks totally unrelated to Perl.  I've had virtually 
no time to spend on this.  I hope to have time this weekend to look at 
Perl related activities (my wife may have other thoughts about that 
however :-))

Anyway, here is the context of what I was trying to learn how to do.

Basically, I wanted to know how to put a receipient's name into a canned 
letter.  Like this.

$name = Joe Blow

The canned letter template:

Dear $name:

Your are a very nice person.

After running the program, the letter would be sent like this:

Dear Joe Blow:

You are a very nice person.

I'll try to cleanup  send the actual code I use for this.  After seeing 
the actual code, I would be very interested in getting your expert advice 
and opinion.  If indeed I'm not doing this in a proper way, I will 
certainly want to correct that. 

Thanks again for providing the feedback.  I greatly appreciate your taking 
the time to help in advancing my Perl knowledge.  I just wish I had more 
time to spend with Perl.  It's such a fun language !

- Stuart

 



 



R. Joseph Newton [EMAIL PROTECTED] 
01/30/2004 12:50 AM

To
[EMAIL PROTECTED]
cc
[EMAIL PROTECTED]
Subject
Re: How to put a variable value into a text file






[EMAIL PROTECTED] wrote:


 Here's what it prints:  My name is $name.

 Here's the testmessage.txt file:  My name is $name.

Hi Stuart,

I think that there is a conceptual problem here.  This really isn't the
appropriate way to get modular code into a program.  Programming would be 
a
dangerous business indeed if our programs automatically interpolated 
variable
names encountered in text data.

The techncal concerns are relatively minor compared to the conceptual
problems.  I would recommend against pursuing this path of inquiry, 
because
the mixing of data and program code is inherently a seed of error and 
system
instability.  It also can be an easy route for subverting system security.
Data and program structures should not be mixed without compelling reason, 
and
a high-end understanding of the potential consequences.  New students of
programming should avoid it entirely, IMHO.

Can you think of a real-world situation where you would want to put 
program
code in a data file?  If you want modular functionality, Perl does have 
some
very approachable protocols for creating packages of such functionality,
including the option of creating instantiable objects to reflect 
real-world
concepts.  For this, you might want to start with:

perldoc perlmod
perldoc perlobj
but first, for necessary background, read
perldoc perlref
or at least
perldoc perlreftut

Use the language features designed for linking functions, though. 
Hand-hcking
mixed code and data will only get you in trouble.

Joseph




Re: How to calculate elapsed time ?

2004-01-28 Thread stuart_clemons
Thanks guys. You're the Best !

I used time instead of localtime and it's exactly what I need for now. 
 Perl is quickly becoming a very useful tool for me.

Thanks also for the doc info: perldoc -f time   perldoc Benchmark

- Stuart

Stuart Clemons/Westford/IBM wrote on 01/27/2004 07:50:30 PM:

 
 Hi all: 
 
 I'm trying to determine how long an system operation takes.  Anyone 
 know of a simple way to do this ? 
 
 I wanted to establish the start time.  Then run the operation.  Then
 mark the finish time.   Then substract the start time from the 
 finish time to get an elapsed time.  Here's the simplistic approach 
 I tried.  I'm sure I need a time that is measured in seconds or 
 something like that, but I'm not sure how to do this. 
 
 TIA 
 
 Here's what I tried: 
 
 #!/usr/bin/perl -w 
 use warnings; 
 use strict; 
 
 my $start = Tue Jan 27 15:40:16 2004; 
 print This is the start time: $start \n; 
 
 system (This is where the system process stuff goes); 
 
 my $finish = localtime; 
 print This is the finish time: $finish \n; 
 
 my $elapsedtime = ($finish - $start) ; 
 print This is the time diff: $elapsedtime \n; 
 
 The above obviously didn't work.  Here's what it returned: 
 
 This is the start time: Tue Jan 27 15:40:16 2004 
 Argument Tue Jan 27 15:40:16 2004 isn't numeric in subtraction (-)
 at C:\Perl\timetest.pl line 13. 
 This is the finish time: Tue Jan 27 19:45:56 2004 
 This is the time diff: 0 
 Argument Tue Jan 27 19:45:56 2004 isn't numeric in subtraction (-)
 at C:\Perl\timetest.pl line ClearCase\Us 

How to calculate elapsed time ?

2004-01-27 Thread stuart_clemons
Hi all:

I'm trying to determine how long an system operation takes.  Anyone know 
of a simple way to do this ?

I wanted to establish the start time.  Then run the operation.  Then mark 
the finish time.   Then substract the start time from the finish time to 
get an elapsed time.  Here's the simplistic approach I tried.  I'm sure I 
need a time that is measured in seconds or something like that, but I'm 
not sure how to do this.

TIA

Here's what I tried:

#!/usr/bin/perl -w
use warnings;
use strict;

my $start = Tue Jan 27 15:40:16 2004;
print This is the start time: $start \n;

system (This is where the system process stuff goes); 

my $finish = localtime;
print This is the finish time: $finish \n;

my $elapsedtime = ($finish - $start) ;
print This is the time diff: $elapsedtime \n;

The above obviously didn't work.  Here's what it returned:

This is the start time: Tue Jan 27 15:40:16 2004 
Argument Tue Jan 27 15:40:16 2004 isn't numeric in subtraction (-) at 
C:\Perl\timetest.pl line 13.
This is the finish time: Tue Jan 27 19:45:56 2004 
This is the time diff: 0 
Argument Tue Jan 27 19:45:56 2004 isn't numeric in subtraction (-) at 
C:\Perl\timetest.pl line ClearCase\Us

How to put a variable value into a text file

2004-01-25 Thread stuart_clemons
Hi all:

I'm trying to put a variable value into a text file.  I've tried a few 
things with no success. 

Anyone know how to do this ?  Is it doable ?  I'm pretty sure I read how 
to do this somewhere, so I'm about to hit the Perl books  to see if I can 
find it. 
Any help will be appreciated.

Here's what I last tried.  It doesn't work and I'm embarrassed to even 
show it.  But, I did want to show that at least I'm trying to make it 
work:

use strict;
use warnings;

open (INFILE, c:\\testmessage.txt) or die The source file failed to 
open - $!;
my @testarray = INFILE;
my $name = stuart;
print @testarray;

Here's what it prints:  My name is $name.

Here's the testmessage.txt file:  My name is $name.

After running my script, I'd like the testmessage.txt to read:  My name is 
stuart.

Thanks again for any help.

Re: How to put a variable value into a text file

2004-01-25 Thread stuart_clemons
Thanks for the help.  I just got back on-line.  I'll give these a try. 
Thanks again !




Randy W. Sims [EMAIL PROTECTED] 
01/25/2004 03:19 PM

To
Jan Eden [EMAIL PROTECTED]
cc
[EMAIL PROTECTED], Perl Lists [EMAIL PROTECTED]
Subject
Re: How to put a variable value into a text file






On 1/25/2004 3:02 PM, Jan Eden wrote:
 Hi Stuart,
 
 @testarray gets the content of testmessage.txt, which contains the 
string '$name'. You cannot manipulate this string by setting the variable 
$name. You could do:
 
 @testarray =~ s/\$name/$name/g;
 
 which will replace the literal string '$name' using your variable's 
content.

or more generally

#!perl

use strict;
use warnings;

my $name = Randy;

while (my $line = DATA) {
   $line =~ s/[^\\](\$\w+)\W/eval $1/eg;
   print $line;
}

__DATA__
My name is $name.

For each line we read in, we look for any occurence of a unescaped 
dollar sign (i.e. not preceeded by a backslash) followed by any 
characters valid in a variable name up to any non-valid character. We 
then substitute that for the current value of that variable within our 
program.

Regards,
Randy.




Re: How to put a variable value into a text file

2004-01-25 Thread stuart_clemons
Thanks Kevin for pointing out the Cookbook receipe.  I dug out my Perl 
Cookbook, 2nd edition.  The How to modify a file in
place without a temporary file is still in Chapter 7, but it's now 
receipe 17.

The discussion for this receipe is very interesting.  I'm not sure if it's 
legal to quote the book, so I'll paraphrase. My read is that this method 
is discouraged, especially if there's other alternatives. The discussion 
mentions a few drawbacks to this method, but the main drawback appears to 
be the potential to eat lots of memory with a large file, though this 
works fine with small files. 

From the the Cookbook, it appears the preferred method is to use a 
temporary file for this sort of task.  (Chapt 7/Receipe 15.)  Basically, 
this method reads the original file, writes changes to a temp file, the 
renames the temp back to the original.  They say the advantages of the 
method are that it uses little memory (compared to not using a temp file), 
as well as being easier and safer to program. 

I learned something here!   Thanks again for pointing out the Cookbook 
receipe. 

- Stuart
  



Kevin Old [EMAIL PROTECTED] 
01/25/2004 03:03 PM

To
[EMAIL PROTECTED]
cc
[EMAIL PROTECTED]
Subject
Re: How to put a variable value into a text file






On Sun, 2004-01-25 at 14:33, [EMAIL PROTECTED] wrote:
 Hi all:
 
 I'm trying to put a variable value into a text file.  I've tried a few 
 things with no success. 
 
 Anyone know how to do this ?  Is it doable ?  I'm pretty sure I read how 

 to do this somewhere, so I'm about to hit the Perl books  to see if I 
can 
 find it. 
 Any help will be appreciated.
 
 Here's what I last tried.  It doesn't work and I'm embarrassed to even 
 show it.  But, I did want to show that at least I'm trying to make it 
 work:
 
 use strict;
 use warnings;
 
 open (INFILE, c:\\testmessage.txt) or die The source file failed to 
 open - $!;
 my @testarray = INFILE;
 my $name = stuart;
 print @testarray;
 
 Here's what it prints:  My name is $name.
 
 Here's the testmessage.txt file:  My name is $name.
 
 After running my script, I'd like the testmessage.txt to read:  My name 
is 
 stuart.
 
 Thanks again for any help.

Hi Stuart,

From the Perl Cookbook Chapter 7, Recipe 10 

open(FH, + FILE) or die Opening: $!;
@ARRAY = FH;
# change ARRAY here
seek(FH,0,0)or die Seeking: $!;
print FH @ARRAY or die Printing: $!;
truncate(FH,tell(FH))   or die Truncating: $!;
close(FH)   or die Closing: $!;

The idea is that you read the file into an array (each element
in the array is a line), modify the lines in the array you'd like to,
then Seek the file position back to the beginning and write the array
to the file, therefore resulting in a modified file.

HTH,
Kevin
-- 
Kevin Old [EMAIL PROTECTED]




RE: Need help with a regex

2004-01-23 Thread stuart_clemons
Thanks very much Tim.  I just did a quick test on my real file and it 
worked perfectly. 

I definitely still have a lot to learn with both Perl and regex's, so I 
really appreciate the explanation as well.  Though your script is very 
compact, I learned a lot from it.  Such as how you initialized the array. 
I have a couple of scripts where I get warnings about either improper or 
uninitialized arrays, or something to that effect.  I tried to fix those, 
but was unsuccessful.  Those scripts produced the output I wanted, but the 
warnings are bothersome. I'll take another look at those scripts to see if 
initializing using my @arrayname = ( ); will help. 

Also, the push structure for adding elements to the array was very 
helpful.  I have a way to do it, and while my way works and is somewhat 
creative, my way is actually really embarrassingly bad  and inefficient 
coding.  So, I learned from that too.

It's funny how all this stuff is in the Perl books that I've been reading, 
but once I need to solve a problem, the exact right way to do it doesn't 
come to me.  I can spend hours trying to do some pretty simple stuff.  I 
can usually come up with a solution, but I know that it's not usually 
efficient nor is it really close to the right way to do it.   But, the 
good news is, if I think about where my Perl skills are today compared to 
a month ago, I'm making progress !

Anyway, sorry for being so looong winded.  The bottom line is that I 
really appreciate your help. 
 



Tim Johnson [EMAIL PROTECTED] 
01/23/2004 01:32 AM

To
Tim Johnson [EMAIL PROTECTED], [EMAIL PROTECTED], 
[EMAIL PROTECTED]
cc

Subject
RE: Need help with a regex






Ooh.  That's embarassing.  I didn't pay close enough attention to the OP. 
Some of the inside matches contain spaces.  My regex should have been:
 
/^\S+\s+(.+)\s+/
 
which would match:

*the beginning of the line (^)
*followed by one or more non-whitespace characters (\S+)
*followed by one or more whitespace characters (\s+)
*followed by one or more of any characters including 
whitespace (.+)
*followed by one or more whitespace characters (\s+)

because Perl will match the largest possible number of characters, the .+ 
will match everything between the two outside spaces.

 -Original Message- 
 From: Tim Johnson 
 Sent: Thu 1/22/2004 9:31 PM 
 To: [EMAIL PROTECTED]; [EMAIL PROTECTED] 
 Cc: 
 Subject: RE: Need help with a regex
 
 

 Try this on for size:
 
 #
 use strict;
 use warnings;
 my @cities = ();
 open(INFILE,myfile.txt) || die Couldn't open 
myfile.txt for reading!\n;
 while(INFILE){
  $_ =~ /^\S+\s+(\S+)/;
  push @cities,$1;
 }
 #do something to @cities
 
 #
 
 which basically means to match:
 
 *   the start of the line (^)
 *   followed by one or more non-whitespace characters 
(\S+)
 *   followed by one or more whitespace characters 
(\s+)
 *   followed by one or more non-whitespace characters 
(\S+)
 
 the parentheses around the last non-whitespace match 
assign it to $1
 
 Note:  Check out perldoc perlre for the man pages.  It 
might be worth looking over real quick before you dig into the book.
 
 Or, for the quick and easy way without a regex, how bout:
 
 #
 
 use strict;
 use warnings;
 my @cities;
 open(INFILE,myfile.txt) || die Could not open 
myfile.txt for reading!\n;
 while(INFILE){
push @cities,(split /\s+/,$_)[1];
 }
 
 #
 
 which does a split on the line and returns the second 
element of the resulting list and assigns it to @cities.
 
 -Original Message-
 From: [EMAIL PROTECTED] 
[mailto:[EMAIL PROTECTED]
 Sent: Thu 1/22/2004 9:01 PM
 To: [EMAIL PROTECTED]
 Cc:
 Subject: Need help with a regex
 
 
 
 This newbie needs help with a regex.  Here's what 
the data from a text
 file looks like. There's no delimiter and the 
fields aren't evenly spaced
 apart.
 
 apples  San Antonio  Fruit
 oranges Sacramento Fruit
 pineapples Honolulu Fruit
  

RE: Need help comparing lines in two files

2004-01-23 Thread stuart_clemons
Thanks Michael.  I'll take a look at those modules and see if my Perl 
skills are sufficient to understand how to use them.  Thanks again.




EUROSPACE SZARINDAR [EMAIL PROTECTED] 
01/23/2004 02:47 AM

To
[EMAIL PROTECTED]
cc
Perl Beginners [EMAIL PROTECTED]
Subject
RE: Need help comparing lines in two files







 Hi Stuart,

Have a look on CPAN (www.cpan.org) there are two wonderfull packages to do
exactely what you are dreaming of :

Algorithm::Diff
Text::ParagraphDiff


Have a nice day
Michel



-Message d'origine-
De: Dan Anderson [mailto:[EMAIL PROTECTED]
Date: vendredi 23 janvier 2004 02:17
À: [EMAIL PROTECTED]
Cc: Perl Beginners
Objet: Re: Need help comparing lines in two files


Lets say file 1 is:

foo
bar
... continues on for 100 lines

And file 2 is:

foo
baz
bar
... continues on exactly the same 100 lines as file 1

Would file 2 be different from file 1 from line 2 and down?  Or would it
be different for line 2 and 3?

Also, the keywords:

next; Brings you to the next iteration in a loop
last; leaves the loop

Should help you iterate through a while loop (or empty loop)

i.e.

{
  # this is a loop, just two sets of brackets
  # put a last statement and it will leave.
  # put one of these in your for loops, or outside of your for loops.
}

Also you can get tricky by naming loops, i.e.:

FOO:
{
  print foo;
  BAR:
  {
last FOO;
  }
  # anything below here never executes
  print bar;
}


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




Re: Need help with a regex

2004-01-23 Thread stuart_clemons
Thanks Jeff.  I hope to try this out later today.  I thought I had the 
solution earlier this morning, but I ran into a problem.  I hope this will 
solve it !  Thanks again.




Jeff 'japhy' Pinyan [EMAIL PROTECTED] 
01/23/2004 10:34 AM
Please respond to
[EMAIL PROTECTED]


To
[EMAIL PROTECTED]
cc
[EMAIL PROTECTED]
Subject
Re: Need help with a regex






On Jan 23, [EMAIL PROTECTED] said:

This newbie needs help with a regex.  Here's what the data from a text
file looks like. There's no delimiter and the fields aren't evenly spaced
apart.

apples  San Antonio  Fruit
oranges Sacramento Fruit
pineapples Honolulu Fruit
lemonsCorona del Rey   Fruit

Basically, I want to put the city names into an array.  The first field,
the fruit name, is always one word with no spaces.

Anyone know how to do that ?

Well, there are many ways.  You could split the string on whitespace,
remove the first and last elements, and join the others with spaces:

  for (@data) {
my @fields = split;
shift @fields;
pop @fields;
push @cities, @fields;  # @array = join( , @array)
  }

Or, you could use a regex that gets SPECIFICALLY what you want:

  for (@data) {
push @cities, $1 if /^\S+\s+(\S+(?:\s+\S+)*)\s+\S+$/;
  }

That regex might need a bit of explanation:

  m{
^ # the beginning of the string
\S+   # one or more non-spaces
\s+   # one or more spaces
( # capture to $1:
  \S+   # first word of the city name
  (?: \s+ \S+ )*# *ALL* remaining words
)
\s+   # one or more spaces
\S+   # one or more non-spaces
$ # the end of the string
  }x;

What this does on a string like peach Georgia fruit is this: the first
\S+\s+ matches peach .  Then we capture Georgia fruit to $1.  However,
the REST of the regex still has to match, but it can't, so the (?:\s+\S+)*
backtracks -- it gives up one of the chunks it matched, so $1 is only
Georgia.  Then the last \s+\S+ can match  fruit.

-- 
Jeff japhy Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
stu what does y/// stand for?  tenderpuss why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]




Need help comparing lines in two files

2004-01-22 Thread stuart_clemons
This very green newbie would like to compare two files, let's say File1 
and File2.   I
want to put the difference from File2 only, into a new file, File3.

For example:

File1.txt
oranges
apples
bananas

File2.txt
apples
kiwi
bananas

The result I want for File3 is the new entry in File2, which is kiwi.  (I
don't care that oranges was in File1 and not File2.)

I tried using a nested foreach loop structure, but I can't get that to 
work and I have a feeling using nested foreach's is not the way to go.

I'm guessing somehow I should use hashs, but I've never used a hash for 
anything and I don't really know how to use a hash.  Can someone help ?

Here's my feeble attempt:

my $file1;
my $file2;

my @file1 = qw(oranges apples bananas);
my @file2 = qw(apples kiwi bananas);

foreach $file2 (@file2){
   foreach $file2 (@file2){
#print $mastervob $tempvob \n;
if ($file2 eq $file1) {
last;   # I would like to go up to the 
toplevel foreach here, but I don't know how to do it
}   # and I'm not sure this would even 
work.
else{
print $file2 \n;
} 
}
}

Re: Need help comparing lines in two files

2004-01-22 Thread stuart_clemons
Thank you Dan and Wolf !   With the suggested changes, my foreach loop 
script now works as I hoped it would.   (My first script did have a typo, 
as you pointed out, though my logic was still wrong.)  I'm glad to be able 
to set aside my study of hashes for another day.  I needed to get this 
problem solved so that I can get some other work done. 

To correct the script, I added LABELS, used the next statement with a 
LABEL, and moved the $print statement out of the inner loop, and waalaa, 
it worked properly.  I quickly went over the logic of the working program 
and it makes sense. It's funny how things seem so clear once they're 
solved ! 

My files are actually probably only 30 to 35 lines each, so size isn't a 
problem.  The real data I'm comparing has email addresses in them.  File2 
will either match File1, or have new email addresses in them.  I then do 
stuff with the new email addresses.

Thanks again.  I really appreciate the help.

Here's the working program:

use strict;
use warnings;

my $file1;
my $file2;

my @file1 = qw(oranges apples bananas);
my @file2 = qw(apples kiwi bananas);

FILE2: foreach $file2 (@file2){
FILE1: foreach $file1 (@file1){
if ($file2 eq $file1) {
next FILE2;
}
}
print $file2 \n; 
}

The output is kiwi, which is exactly right. 
kiwi




wolf blaum [EMAIL PROTECTED] 
01/22/2004 08:38 PM

To
[EMAIL PROTECTED], [EMAIL PROTECTED]
cc

Subject
Re: Need help comparing lines in two files






 This very green newbie would like to compare two files, let's say File1
 and File2.   I
 want to put the difference from File2 only, into a new file, File3.

I had a very simliar problem about a week ago, which James answerd here:

http://groups.google.com/groups?q=Perl+looping+(a+lot+of)
+fileshl=enlr=ie=UTF-8selm=28A16704-4AD3-11D8-9A03-000A95BA45F8%
40grayproductions.netrnum=1

or try google groups perl looping through (a lot of) files

The only really difference is that I didnt want to compare one FILE2 to 
FILE1 
but 500.

However, be carefull on your filesize:
I settled reading one file into mem (as an array) and looping through the 
other ones using a while (FILE2)  reading the 500 files line by line.

 For example:

 File1.txt
 oranges
 apples
 bananas

 File2.txt
 apples
 kiwi
 bananas

 The result I want for File3 is the new entry in File2, which is kiwi. (I
 don't care that oranges was in File1 and not File2.)

 I tried using a nested foreach loop structure, but I can't get that to
 work and I have a feeling using nested foreach's is not the way to go.

why not?

 I'm guessing somehow I should use hashs, but I've never used a hash for
 anything and I don't really know how to use a hash.  Can someone help ?

do you need to associate the contens of the line with a filename ore 
something? if not, use an array.

 Here's my feeble attempt:

 my $file1;
 my $file2;

 my @file1 = qw(oranges apples bananas);
 my @file2 = qw(apples kiwi bananas);

As Dan showed:

FILE2:
 foreach $file2 (@file2){
foreach $file2 (@file2){
you may want 
 foreach my $file1(@file1){ 
here

 #print $mastervob $tempvob \n;
 if ($file2 eq $file1) {
 last;   # I would like to go up to the
 toplevel foreach here, but I don't know how to do it
 }   # and I'm not sure this would 
even
 work.

as Dan said:
 
 next FILE2;

will do the job.


 else{
 print $file2 \n;
 }
 }
 }

This doesent do what I assume you want: when you place the print in the 
inner 
loop.
Just look at the link above.

Hope thats a start, Wolf




Need help with a regex

2004-01-22 Thread stuart_clemons
This newbie needs help with a regex.  Here's what the data from a text 
file looks like. There's no delimiter and the fields aren't evenly spaced 
apart. 

apples  San Antonio  Fruit
oranges Sacramento Fruit
pineapples Honolulu Fruit
lemonsCorona del Rey   Fruit 

Basically, I want to put the city names into an array.  The first field, 
the fruit name, is always one word with no spaces.

So, I would guess that the regex needs to grab everything after the first 
word and before the beginning of Fruit.  Then strip out all the spaces. 

Or grab the beginning of the second word until the beginning of Fruit. 
Then strip out the spaces after the city name. 

Anyone know how to do that ?

I did recently buy the Mastering Regular Expressions, 2nd Edition book. 
I've only read a little, but I've found the book to be very readable.  If 
I only had the time to really spend with it !  So much to learn, so little 
time.

Thanks in advance for any help.

Question about CPAN's Text::Diff

2004-01-07 Thread stuart_clemons




Hi all:

This newbie would like to compare two files, let's say File1 and File2.   I
want to put the difference from File2 only,  into a new file, File3.

For example:

File1.txt
oranges
apples
bananas

File2.txt
apples
kiwi
bananas

The result I want for File3 is the new entry in File2, which is kiwi.  (I
don't care that oranges was in File1 and not File2.)

Can the Text:Diff module (or some other means) do this ?  (I looked at the
Text::Diff doc and it wasn't obvious to me.  I hope to play with this later
this evening or tomorrow.)

Thanks in advance for your help (and for your past help as well !)

- Stuart


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




Newbie trying to cleanup/format text file

2003-12-22 Thread stuart_clemons
Hi all:

I'm trying to cleanup and format this text file of user names, so that I 
have one column of user names.  Here's the text file:

The request will be processed at a domain controller for domain FOOBAR.

Group name  Misc
CommentThese are the FOOBAR users

Members

---
arod cschilling   csenior 
ecostelloffrank   gbennett 
LBirdPMartinez 
The command completed successfully.

I would like an output file to read like this:

arod
cschilling
csenior
ecostello
ffrank
gbennett
LBird
PMartinez

I know how to put the names into one column, if I don't have all the extra 
lines.  So, I've been trying to figure out how to eliminate all lines 
except for the names.

I tried opening the file, then using a not match regex to eliminate all 
lines except the name lines.  So far, I can't seem to eliminate more than 
one line of not match.   Any help will be appreciated.  Here's my feeble 
newbie code.  This code will eliminate the Group line, but that's it.  I 
tried adding other conditionals to eliminate other unwanted lines, but 
they don't work.

open(FH, c:\\foobar.txt) or die sourcefile open failed - $!;
while (FH){
if (not $_ =~ /^Group/) {
print $_ ;
}
} 

re: Newbie trying to cleanup/format text file

2003-12-22 Thread stuart_clemons
I kept working at it and was finally able to get rid of the unwanted lines 
in the text file using a series of nested if's.  It's certainly not 
elegant, but it works !

Surely there's a more efficient way to get rid of the unwanted lines. 

 Anyway, here's what I came up with for now.  This leaves the names in 
one, two, or three columns, but I know how to reformat them into one name 
per column.

#!/usr/bin/perl -w
use strict;
$_ = ;
open(INFILE, c:\\foobar.txt)or die sourcefile open failed - $!;

while (INFILE){
 if (not $_ =~ /^The request/){
  if (not $_ =~ /^Group/){
   if (not $_ =~ /^Comment/){
if (not $_ =~ /^Members/){
 if (not $_ =~ /^--/){
  if (not $_ =~ /^The command/){
   if ($_ =~ /\S/){
print $_ ;
   } 
  } 
 } 
}
   }
  }
 }
}
close

The result of this code is this:

arod cschilling   csenior 
ecostelloffrank   gbennett 
LBirdPMartinez 

- Forwarded by Stuart Clemons/Westford/IBM on 12/22/2003 01:47 PM 
-

Stuart Clemons/Westford/IBM 
12/22/2003 11:16 AM

To
[EMAIL PROTECTED]
cc

Subject
Newbie trying to cleanup/format text file







Hi all: 

I'm trying to cleanup and format this text file of user names, so that I 
have one column of user names.  Here's the text file: 

The request will be processed at a domain controller for domain FOOBAR. 

Group nameMisc 
CommentThese are the FOOBAR users 

Members 

--- 

arod cschilling   csenior 
ecostelloffrank   gbennett 
LBirdPMartinez 
The command completed successfully. 

I would like an output file to read like this: 

arod 
cschilling 
csenior 
ecostello 
ffrank 
gbennett 
LBird 
PMartinez 

I know how to put the names into one column, if I don't have all the extra 
lines.  So, I've been trying to figure out how to eliminate all lines 
except for the names. 

I tried opening the file, then using a not match regex to eliminate all 
lines except the name lines.  So far, I can't seem to eliminate more than 
one line of not match.   Any help will be appreciated.  Here's my feeble 
newbie code.  This code will eliminate the Group line, but that's it.  I 
tried adding other conditionals to eliminate other unwanted lines, but 
they don't work. 

open(FH, c:\\foobar.txt) or die sourcefile open failed - $!; 
while (FH){ 
if (not $_ =~ /^Group/) { 
print $_ ; 
} 
} 

Recommended simple Perl IDE/Editors ?

2003-12-15 Thread stuart_clemons
Hi all:

Just wondering what IDE/editor folks use for their Perl work ?

I'm looking for something for a W2K system that is easy to use (without 
customization) and that has the basics (syntax highlighting, visual 
debugging, something that shows variable values, the values in whitespace, 
etc) without a lot of bell  whistles.

I plan to look at:

ActiveState Komodo 2.5
PerlEdit
OpenPerl IDE

Any recommendation, or feedback on the above IDE's would be appreciated.

Thanks.



Re: sorter script [was: Frustrated newbie question]

2003-12-07 Thread stuart_clemons
Hi drieux:

The link that failed for you, worked for me.  That link led to this link 
which had the code.

http://products.daviddurose.com/cgi-bin/download.cgi?script=sort


- Message from drieux [EMAIL PROTECTED] on Sun, 7 Dec 2003 09:04:51 
-0800 -
To:
Perl Perl [EMAIL PROTECTED]
Subject:
Re: sorter script [was: Frustrated newbie question]

On Dec 7, 2003, at 3:31 AM, John W. Krahn wrote:
[..]

 http://www.downloaddatabase.com/databasesoftware/db-sorter-script.htm

 Why?

more importantly,

How?

I have tried a couple of times to download it
and get nothing. the retreat to the handy dandy

GET -d -u -U -s -S -e 
http://www.downloaddatabase.com/databasesoftware/download-db-sorter- 
script.htm

is not giving me any insight into what is going on...


ciao
drieux

How to process multi-line records ?

2003-12-07 Thread stuart_clemons
Hi all:

This newbie is back looking for some hints on how to handle this problem 
in processing a file that has multiline records that look something like 
this:

Name:  Joe Blow
DataField1:  x
DateField1: 07/07/77
DataField2: x
DateField2: 12/07/03

Name:  Fi Doe
DataField1:  x
DateField1: 08/08/88
DataField2: x
DateField2: 12/12/03

etc.

There is an empty line that separates each record.   I need to extract the 
records that meet certain criteria.   I would really like to know how to 
do this in Perl.
(For a simple task like this, in the past, I would just import these 
records into a database and write a query to extract the records that I 
wanted.) 

I've actually thought a lot about the problem, but I haven't done any perl 
coding that would allow me to put my woeful perl ineptitude on public 
display in this forum.   I hope to find the time to start on this problem 
in the next couple of days.  As I'm certain to run into problems, I will 
then share my ineptitude while looking for proper guidance from the valued 
learned ones.

Until then, here's what I was thinking to do.  What I thought I would do 
is try to read each line of one record into an array or hash, then use a 
conditional to determine if that record meets the desired criteria.  If it 
meets the criteria, write out this record to another  file and then read 
in the next record.  If the record doesn't meet the criteria, read in the 
next  record.  I would keep doing this until EOF.

I think I can handle the conditional stuff, but what I don't know how to 
do is read in each line of the record and stop reading when I hit the 
empty line so that I can do the conditional stuff.

Is this a valid approach to take for this problem?  And if so, should I 
use an array or hash ?   And again, how do I stop reading the input file 
when I hit the empty line so that I can do the conditional stuff ?

Thanks in advance for any help and/or hints.  The feedback from my last 
question was extremely helpful as I struggle to get Perl to do what I want 
it to do. I think I'm making progress, although it doesn't always feel 
that way ! 

- Stuart


Frustrated newbie question

2003-12-05 Thread stuart_clemons




Help. I'm a frustrated newbie who wants to use Perl to make my life easier.

The following simple task is only one small part of a program I'm trying to
put together to automate some things I currently do manually.

I have a file whose format looks like this:

name1  name2  name3
name4  name5  name6, etc.

The names are separated by spaces.   I need the names to be one name per
line, like this:

name1
name2
name3, etc.

I currently use a macro with a text editor to clean up the file into the
one name per line format.  I can do this very quickly in contrast to the
the last two hours I've spent trying to figure out how to get Perl to do
this very simple task.  Arrggh !

To simply things, I just tried to take the following string and print it
out one name per line.

my $x = name1 name2 name3;

I've tried various schemes using regex's and the ///s operator.  Most of
the time I get syntax errors and the few times I get anything to work, it's
not what I want.

I did get this array structure to work:

my @names = qw(name1 name2 name3);
print $names[0] \n;
print $names[1] \n;
print $names[2] \n;

So I then spent time unsuccesfully trying to figure out how to get my
string split into the array. I couldn't get that to work either. More
Arrggh !

Anyway, any help at this point will be appreciated.  I'm hoping that in the
long run the time I spend learning Perl will pay off, which it will if I
can automate some of the tasks I do manually (with the help of macros in a
text editor).

My next Perl task after I get my list of one name per line, is to sort the
list and eliminate duplicate names.

Thanks again for any help.


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




re: Frustrated newbie question

2003-12-05 Thread stuart_clemons




Thanks to all with the quick responses and possible solutions for splitting
my names list into one name per line.  I've tried a couple of them and they
work great.  You guys make it look so easy !

Using an array with 'Split' seemed to be a key part of the solution.  I
guess I was sort of on the right track.  though I'm not sure when (or
if) I would have figured it out.

Anyway, thanks again to everyone for the help and support.  I appreciate
it.

- Stuart


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