Re: Changing case of the first letter of words in string

2003-08-22 Thread David K. Wall
Bis [EMAIL PROTECTED] wrote:
I want to make the case of the first letter of all the words in a
selected string to upper case. The code
s/\b(\w+)/\u$1\E/g;

enables me to do this for the whole document.

But the string I want to match and operate on is all instances of text
following the string
SCTN:

as in

SCTN: News Analysis

or

SCTN: Special Report

But when I try

s/(SCTN:\s*)\b(\w+)/$1\u$2\E/g;

nothing seems to change? : (
That will only change the first letter of the first word after 'SCTN:'. 
That's what you're seeing, right?  If you want to change *all* the words 
after SCTN: to start with uppercase (and leave the ones before it alone), 
maybe something like this:

my $text = q(leave this alone SCTN: this isn't capitalized but should be);
if (/SCTN:/) {
   my ($before, $after) = split /SCTN:/, $text, 2;
   $after =~ s/(\S+)/\u$1/g;
   $text = $before . 'SCTN:' . $after;
}
print $text;
I used \S instead of \w in an attempt to handle contractions such as 
can't and don't, etc.

Now I'd almost bet someone (John Krahn?) will come up with a more clever 
approach and make it into a one-liner. :-)



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: Simple question

2003-08-14 Thread David K. Wall
Trevor Morrison [EMAIL PROTECTED] wrote:

I am trying to compare two arrays to find  common numbers in both.  For
the numbers that are not common to both, I want to write them to a file
for my review.
Check the FAQ:

perldoc -q difference of two arrays

How do I compute the difference of two arrays? How do I compute the 
intersection of two arrays?



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: How do I return more than 1 array from a sub routine ?

2003-08-14 Thread David K. Wall
T.S.Ravi Shankar [EMAIL PROTECTED] wrote:
Hi all :

How could I return more than 1 array or hash from a sub routine 
collect them in different arrays or hashes in the calling program ??
Return references to the arrays.  Here's an example:

sub demosub {
   my @array1 = 1..10;
   my @array2 = 'a'..'f';
   return [EMAIL PROTECTED], [EMAIL PROTECTED];
}
my ($aref1, $aref2) = demosub();
print join(', ', @$aref1), \n,  join(', ', @$aref2), \n;
See 'perldoc perlref' for considerably more information about references.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: How to replace a text in a file

2003-08-14 Thread David K. Wall
Vinay Thombre [EMAIL PROTECTED] wrote:
[attribution inserted]
Jeff 'japhy' Pinyan [EMAIL PROTECTED] wrote
  open INPUT,  in.txt or die can't read in.txt: $!;
  open OUTPUT,  out.txt or die can't write out.txt: $!;
  while (my $line = INPUT) {
# do something to $line
print OUTPUT $line;
  }
  close OUTPUT;
  close INPUT;
I want in.txt and out.txt file to be same.
That is I want to replcae text insa me file and do not want to create a
new file.
perldoc -q insert

How do I change one line in a file/delete a line in a file/insert a line in 
the middle of a file/append to the beginning of a file?

Use the Tie::File module, which is included in the standard distribution 
since Perl 5.8.0.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: [solved but..] Push first 9 elements

2003-08-01 Thread David K. Wall
Janek Schleicher [EMAIL PROTECTED] wrote:
John W. Krahn wrote at Thu, 31 Jul 2003 00:22:04 -0700:

If you are just removing one element then you can use shift instead.
And you are not really using $i it seems.
for ( 0 .. @archivo - 4 ) {
  push @lista_final, shift @correos_p;
  push @lista_final, shift @correos_h;
  push @lista_final, shift @correos_y;
  push @lista_final, shift @correos_l;
  push @lista_final, shift @correos_t;
  push @lista_final, shift @correos_s;
  push @lista_final, shift @correos_o;
}
Let's shorten that a bit :-)

for ( 0 .. @archivo - 4 ) {
   push @lista_final, shift @{correos_$_} for qw/p h y l t s o/;
}
That might be one of the rare moments where it could be correct to use
symbolic references. Here it avoids having multiplied the code and logic 7
times. And IMHO it makes the code more readable as it is easier to follow
the main idea :-)
You don't need symbolic references.

for ( 0 .. @archivo - 4 ) {
   push @lista_final, shift @$_ for (
   [EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED],
   [EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED]
   );
}
I agree with Steve Grazzini, though, and suspect there may be a better way 
to represent the data.  But since we don't know what the data is, we can't 
suggest anything.

--
David Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: pattern matching

2001-11-19 Thread David K. Wall

[EMAIL PROTECTED] (Andrea Holstein) wrote:

 Prasanthi Tenneti wrote: 
 
 Iam a beginner in perl.I have one question, Iam trying to write one
 prog,in which i have to search for one word in a file, If I found that
 word,print next 4 lines. PLs help me,how to write code. 
 
 open FILE, filename; 

Always test the result of open().

open FILE, filename or die Cannot open file: $!;


 while (FILE) {
  print(FILE,FILE,FILE,FILE), last if /your_word/;
 }
 close FILE;

Did you test that?  

print FILE;

will print the rest of the file.

print() expects a list.   in list context returns rest of whatever file 
it's reading, one line per list element. 

The same question -- with a trivial difference -- was asked a few days 
ago.

use strict;
use warnings;
open FILE, filename or die Cannot open file: $!;
while (FILE) {
next unless /your_word/;
$_ = FILE, print for 1..4;
last;
}
close FILE, filename or die Cannot close file: $!;


Alternately, the loop could be rewritten to be (perhaps) a little clearer:

while (FILE) {
next unless /your_word/;
for (1..4) {
$_ = FILE;
print;
}
last;
}

-- 
David Wall - [EMAIL PROTECTED]
When the end of the world comes, I want to be in Cincinnati. Everything
happens ten years later there. -- Mark Twain

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]