473,385 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

substitution in files

Kelicula
176 Expert 100+
I am trying to open a file, and search for a srting. Once found I want to substitute it for something else and acually change the file.

ie) File named "stuff.txt" contains:
Expand|Select|Wrap|Line Numbers
  1. Hello I am Kelly
  2. Hello I am David
  3. Hello I am mark
  4.  
I want to find David and change it to Josh.

This is how I am trying to do it, but it doesn't work.

Expand|Select|Wrap|Line Numbers
  1. open (FIL, "+<stuff.txt") or die "$!";
  2.  
  3. foreach $line (<FIL>){
  4.  
  5. if($line =~ m/David/){
  6. $line =~ s/David/Josh/;
  7. }
  8. print $line;
  9. }
  10.  
What am I doing wrong?
I tried opening the file with ">>" and "+>>", to no avail.

???
Aug 24 '07 #1
7 1390
Kelicula
176 Expert 100+
I have solved this problem.
I load the lines into an array and then replace the line that matches.
ie)
Expand|Select|Wrap|Line Numbers
  1. open (INFO, "<stuff.txt") or die "$!";
  2.  
  3. @w =<INFO>;
  4. close (INFO);
  5.  
  6. foreach $i (@w){
  7. if ($i =~ m/David/){
  8. $i =~ s/David/Josh/;
  9. }
  10. }
Now what I need is a way to disearn if it has been 24 hours since the last entry???


whatever...

I know it's too complicated to post here.
Aug 25 '07 #2
miller
1,089 Expert 1GB
Instead of loading the entire file into memory, I suggest that you take advantage of Tie::File for editing a file.
perlfaq5 - 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?
- Miller
Aug 25 '07 #3
Kelicula
176 Expert 100+
Thanks! because the file in question is going to get pretty big.
I am creating a survey for people to vote on a band. But the owner (my boss) wants people to be able to vote every 24 hrs. So my next challange is to be able to see if the time is greater than or less than 24 hrs from the last time they voted. (assuming the same ip address has voted before)
This has stopped me in my tracks.... :0 wft?
How in the world am I gonna do that? the date is formatted to a string and a number...
here's the page:
http://www.liveaudiomag.com/survey.shtml

right now all it does is display the file that I append to. I'm in the process of searching for ip and replacing line if found and greater than 24 hours ago. But wft? How am I going to be able to tell if they voted greater than 24 hours ago?


any help is greatly appreciated!!
Aug 25 '07 #4
KevinADC
4,059 Expert 2GB
when a person votes store the time they voted in epoch seconds using the time function:

$voted = time;

store that along with any other information you are already storing. Then check to see if the time is less than 24 hours. 24 hours would need to be broken down into seconds:

60x60x24=86400

so:

Expand|Select|Wrap|Line Numbers
  1. if ( $time+86400 < time) {
  2.    less than 24 hours has passed
where $time is the time you stored previously
Aug 25 '07 #5
Kelicula
176 Expert 100+
KevinADC, you are truly brilliant!
Thanks, it works fine.
I am acually going to be able to have this thing running by the end of the weekend.
The Tie::File was a great help too.

Much Thanks!
Aug 25 '07 #6
Kelicula
176 Expert 100+
I noticed in the Tie file page it mentions that concurrent access to files is inappropriate.

What would happen if several people were voting at the same time?
Aug 25 '07 #7
KevinADC
4,059 Expert 2GB
Look for "flock" in the Tie::File documentation. Thats about all you can do with a text file.
Aug 25 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Paddy | last post by:
Hi, I got tripped up on the way eval works with respect to modules and so wrote a test. It seems that a function carries around knowledge of the globals() present when it was defined. (The...
0
by: EJ | last post by:
Is there a way to specify a substitution variable that can be used in several places in the app.config file (like you can do in Ant/NAnt build files)? For example, if there is part of a file path...
1
by: kollareddy | last post by:
Hi all, I am new to xml/xsd world. I want to know the differences between complex type and element being abstract and if both can be declared so, in case of substitution goups.Also can xsi:type...
4
by: Don | last post by:
I think "macro substitution" is the correct term for what I want to do, but, to be sure, here is a description of what I'd like to know is possible: I want to be able to create a create an object...
18
by: james | last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits the "," and then copies the results into a listbox. The data also...
5
by: Murali | last post by:
In Python, dictionaries can have any hashable value as a string. In particular I can say d = {} d = "Right" d = "Wrong" d = "test" In order to print "test" using % substitution I can say
4
by: Ian | last post by:
Hi, Hopefully a simple question but my brain is hurting... I want to make a regex substitution, using search and replace patterns contained in variables. What I want to do is: $f =...
2
by: Bilal | last post by:
Hello, I'm stuck on this problem for quite some time and hope somebody would be able to guide me. Basically, I need to populate a large number of "template" XML files which have all...
2
by: viratgite | last post by:
Hi, I am trying to substitute C:\Program Files\ABC\ with C:/Program Files/ABC/ or C:\\Program Files\\ABC\\ but somehow it doesn't work. It replaces / with null and whole output becomes C:Program...
2
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
In our application consisting of several custom user controls we want one of these controls to be excluded from the page cache. For this I tried to use the substitution control for output cache...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.