LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 08-08-2001, 09:07 AM   #1
zikhermm
Member
 
Registered: Jul 2001
Location: New York City
Distribution: Suse, Debian, Slack
Posts: 49

Rep: Reputation: 15
Perl question (brain not functioning)


I have a text file which contains, among other words, the words "version" and "revision". I need to convert them to titlecase. This is what I have so far:

open(MYFILE, "test.dat") || die "Can't open test.dat!\n";
while ($line = <MYFILE>){
s/version|revision/\u$&/g;
}
close(MYFILE);


My questions is: How would I write the changes to the file? I have a feeling it's something simple, but I haven't used Perl in about a year, and somehow I can't recall the way to go about doing this.
 
Old 08-08-2001, 09:35 AM   #2
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
Re: Perl question (brain not functioning)

Quote:
Originally posted by zikhermm
open(MYFILE, "test.dat") || die "Can't open test.dat!\n";
while ($line = <MYFILE>){
s/version|revision/\u$&/g;
}
close(MYFILE);
How about something like
Code:
#!/usr/local/bin/perl -w
$filename = "test.dat";

#open the file and read the whole thing into an array,
#afterall memory is cheap!
open(MYFILE, $filename) or die "Cannot open file $filesname !!!\n";
@myArray = (<MYFILE>);
close(MYFILE);

#open the file again for writing - this will truncate the file
#to zero length.
open(MYFILE, "> $filename") or die "Cannot open file $filename for output";

#print line in the array out to your 'new' file
foreach $line (@myArray) {
    #I'm assuming your regex is correct!
    $line =~ s/version|revision/\u$&/g;
    print MYFILE $line;
}
#now close the file
close(MYFILE);
HTH

Jamie...
 
Old 08-08-2001, 06:00 PM   #3
zikhermm
Member
 
Registered: Jul 2001
Location: New York City
Distribution: Suse, Debian, Slack
Posts: 49

Original Poster
Rep: Reputation: 15
Is there any other way to do it? I hate to waste memory. In Perl, TMTOWTDI
 
Old 08-09-2001, 04:58 AM   #4
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
Another way - yeah sure! Use something like your original code and output to a second file
Code:
#!/usr/loca/bin/perl -w

$inputFile = "test1.dat";
$outputFile = "test2.dat"

open(IN, $inputFile) or die "Can't open $inputFile\n";
open(OUT, "> $outputFile") or die "Can't open $outputFile\n";
while ($line = <IN> ){
    s/version|revision/\u$&/g;
    print OUT $_;
}
close(IN);
close(OUT);

unlink($inputFile) or die "Unable to unlink $inputFile \n";
rename($outputFile, $inputFile) or 
  die "Unable to rename $outputFile to $inputFile \n";
I'd still use the first version if I were you, it should be faster as you are only making a single larger disk access at the read stage, and the memory will only be used during the run time of the script. BTW I havn't actually run the above code - its all off the top of my head, but I'm sure you'll get the jist of it...

cheers

Jamie...
 
Old 08-09-2001, 09:09 AM   #5
zikhermm
Member
 
Registered: Jul 2001
Location: New York City
Distribution: Suse, Debian, Slack
Posts: 49

Original Poster
Rep: Reputation: 15
Thanks, jharris. That's actually what I thought of myself after I thought about the problem a little more. By the way, in clear English, what does

$some_variable = shift;

exactly do?
Thanks for your help.
 
Old 08-09-2001, 09:42 AM   #6
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
You can use the shift command to get parameters that are passed to a subroutine. For example I often use the following to remove leading and trailing white space from parameters
Code:
sub*trim*{
  **my*($str)*=*shift;
  **if*($str)*{
    ****$str=~s/^\s+|\s+$//g;
    ****return*$str;
****}
}
If you passed two parameters to the subroutine then you would call shift twice, once for each parameter like
Code:
$someVar = shift;
$someOtherVar = shift;
BTW the use of 'my' effectively creates a local variable.

HTH

Jamie...
 
  


Reply


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
Linux in Brain Surgery rvijay Linux - News 4 07-22-2005 07:27 PM
Rat Brain Pilot AvePtah General 9 11-05-2004 09:14 PM
Hiding code in PERL, perl gui question randomx Programming 1 06-26-2004 04:22 PM
brain upgrade needed macewan General 24 05-06-2003 07:35 PM
help! my brain is melting! briancof Linux - Newbie 5 11-26-2001 04:35 PM


All times are GMT -5. The time now is 07:30 AM.

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