CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Urgent Help

 



manojrayan
New User

Sep 22, 2011, 12:24 PM

Post #1 of 7 (4313 views)
Urgent Help Can't Post

Objective: Find word(keyword) and convert to Uppercase.
I've a code(code.txt file) for which i've stored the keywords in the keyword.txt file. I need to read the code.txt file and print all ,if a match is found in keyword.txt i need to make that specific keyword to Uppercase. Since im very need i need your help to do this.

Sample code.txt:

data output;
set input;
if varname=1 then var_flag=1;
else var_flag=0;
run;

sample keyword.txt
data
set
if
then
else
run

My code can be min. of 200 lines and can be upto 2000 lines.

expected output
new_code.txt

DATA output;
SET input;
IF varname=1 THEN var_flag=1;
ELSE var_flag=0;
RUN;


wickedxter
User

Sep 22, 2011, 3:14 PM

Post #2 of 7 (4309 views)
Re: [manojrayan] Urgent Help [In reply to] Can't Post

 

Code
 
my @words = qw(data set);

open FILE1, "file.txt";
my @file = <FILE1>
close FILE1;

open FILE2, ">>file2.txt";
foreach (@file){
foreach my $word (@words){
if($word eq $_){
print FILE2 uc($_);
}else {
print FILE2 $_;
}
}
}
close FILE2

NOTE: untested this is a baisc attempt this is used for small files to search.

depending on what word your looking for you might have to adjust the if statement to better suit your needs.


(This post was edited by wickedxter on Sep 22, 2011, 3:17 PM)


manojrayan
New User

Sep 22, 2011, 11:46 PM

Post #3 of 7 (4302 views)
Re: [wickedxter] Urgent Help [In reply to] Can't Post

Thanks a lot for your help.
i'll try this and let you know.


rovf
Veteran

Sep 23, 2011, 12:07 AM

Post #4 of 7 (4299 views)
Re: [manojrayan] Urgent Help [In reply to] Can't Post


Quote
My code can be min. of 200 lines and can be upto 2000 lines.


Why?


manojrayan
New User

Sep 23, 2011, 12:15 AM

Post #5 of 7 (4297 views)
Re: [rovf] Urgent Help [In reply to] Can't Post

that's just an information, not a limitation. :)


manojrayan
New User

Sep 23, 2011, 5:48 AM

Post #6 of 7 (4251 views)
Re: [wickedxter] Urgent Help [In reply to] Can't Post

I tried this,

1. Every line is printed twice since we are searching 2 keywords "data set" . Also it's printing the whole line instead of word by word.

2. print file2 uc($_) - is not printing in UPPERCASE into the file2. But when i tested to print in screen it's printing.

i need to print word by word. if it matches with keyword i need uppercase (only the matched word). else the non matching word should be printed in lowercase

Appreciate your help.


Chris Charley
User

Oct 7, 2011, 6:03 PM

Post #7 of 7 (3691 views)
Re: [manojrayan] Urgent Help [In reply to] Can't Post

This may be a solution.

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

my $code = <<EOF;
data output;
set input;
if varname=1 then var_flag=1;
else var_flag=0;
run;
EOF

my $keywords = <<EOF;
data
set
if
then
else
run
EOF

my $keyword;
{
local $/; # slurp the keyword file (in this limited scope)
open my $kw, "<", \$keywords;
$keyword = join "|", split ' ', <$kw>;
close $kw;
}

open my $c, "<", \$code;
while (<$c>) {
s/($keyword)/uc $1/eg;
print;
}
close $c;
__END__

{
local $^I = '.bak';
local @ARGV = ("code.txt", ..., ... );
while (<>) {
s/($keyword)/uc $1/eg;
print;
}
}

The code beyond the __END__ would do inplace editing if this bit of code replaced the code above for the while loop. It is also contained in a scope, (between the opening brace { and the closing brace }.


(This post was edited by Chris Charley on Oct 9, 2011, 6:03 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives