473,396 Members | 1,805 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,396 software developers and data experts.

Problem editing a file

Hello,

I hope someone can help me with this!
I have a problem editing a text file my file looks like this

11
13
15
16
23

I want to read the file a line at a time and add 0.5 to every line so this is the script i used.

Expand|Select|Wrap|Line Numbers
  1. open(my $a, '>File.txt');
  2. $b ="\t0.5\n";
  3.     open (Data, 'Data.txt');
  4. while ($c = <Data>){
  5.  
  6. print $a "$c\t$b";
  7. }
  8.  
  9. close $a
  10. close (Data)
I want my output to look like this

11 0.5
13 0.5
15 0.5
16 0.5
23 0.5

but it looks like this

11
0.5
13
0.5
15
0.5
16
0.5
23
0.5

How do i get my data printed on one line?
Oct 22 '07 #1
10 1358
numberwhun
3,509 Expert Mod 2GB
Hello,

I hope someone can help me with this!
I have a problem editing a text file my file looks like this

11
13
15
16
23

I want to read the file a line at a time and add 0.5 to every line so this is the script i used.

Expand|Select|Wrap|Line Numbers
  1. open(my $a, '>File.txt');
  2. $b ="\t0.5\n";
  3.     open (Data, 'Data.txt');
  4. while ($c = <Data>){
  5.  
  6. print $a "$c\t$b";
  7. }
  8.  
  9. close $a
  10. close (Data)
I want my output to look like this

11 0.5
13 0.5
15 0.5
16 0.5
23 0.5

but it looks like this

11
0.5
13
0.5
15
0.5
16
0.5
23
0.5

How do i get my data printed on one line?
Try adding the following two lines to your code before printing to the new file:

Expand|Select|Wrap|Line Numbers
  1. chomp($a);
  2. chomp($b);
  3. chomp($c);
  4.  
Regards,

Jeff
Oct 22 '07 #2
I tried this but it works for some of the numbers but not all. The output looks like this i want a tab in between the numbers.But i only get it for numbers who have two digits, if they have only one i get just a space.

[HTML]
11 0.5
12 0.5
1 0.5
2 0.5
5 0.5
22 0.5
44 0.5[/HTML]
Try adding the following two lines to your code before printing to the new file:

Expand|Select|Wrap|Line Numbers
  1. chomp($a);
  2. chomp($b);
  3. chomp($c);
  4.  
Regards,

Jeff
Oct 22 '07 #3
mehj123
55
I tried this but it works for some of the numbers but not all. The output looks like this i want a tab in between the numbers.But i only get it for numbers who have two digits, if they have only one i get just a space.

[HTML]
11 0.5
12 0.5
1 0.5
2 0.5
5 0.5
22 0.5
44 0.5[/HTML]
Hi..
Your code is working fine for me... and I am gettting equal space between the numbers no matter if they are one digit or 2 digit.

Expand|Select|Wrap|Line Numbers
  1.  
  2. open(my $a, '>File.txt');
  3. $b ="\t0.5\n";
  4. open (Data, 'Data.txt');
  5. while ($c = <Data>){
  6. chomp($c);
  7. print $a "$c\t$b";
  8. }
  9.  
  10. close $a;
  11. close (Data);
  12.  
I am attaching the input and output files....
Oct 22 '07 #4
Hi..
Your code is working fine for me... and I am gettting equal space between the numbers no matter if they are one digit or 2 digit.

Expand|Select|Wrap|Line Numbers
  1.  
  2. open(my $a, '>File.txt');
  3. $b ="\t0.5\n";
  4. open (Data, 'Data.txt');
  5. while ($c = <Data>){
  6. chomp($c);
  7. print $a "$c\t$b";
  8. }
  9.  
  10. close $a;
  11. close (Data);
  12.  
I am attaching the input and output files....
If tried it several times om my data but it only gives a tab at the largest number of digits i have different numbers of digits in my data and only the one's with the most digits get's a tab in between my output keeps looking like this.

[HTML]1234 0.5
123 0.5
12 0.5
1234 0.5
1 0.5
1234 0.5
1234 0.5[/HTML]
Oct 22 '07 #5
My data is in a text file could that be a problem should it be in another format?
Oct 22 '07 #6
hi,
i worked with this code, it is working fine as like your required output.

Expand|Select|Wrap|Line Numbers
  1. open(my $a, '>File.txt');
  2.       $b ="0.5\n";
  3.       open (Data, 'Data.txt');
  4.       while ($c = <Data>){
  5.       chomp($c);
  6.       print $a "$c $b";
  7.       }
  8.       close $a;
  9.       close (Data);
  10.  
Oct 23 '07 #7
Hi..
Your code is working fine for me... and I am gettting equal space between the numbers no matter if they are one digit or 2 digit.

Expand|Select|Wrap|Line Numbers
  1.  
  2. open(my $a, '>File.txt');
  3. $b ="\t0.5\n";
  4. open (Data, 'Data.txt');
  5. while ($c = <Data>){
  6. chomp($c);
  7. print $a "$c\t$b";
  8. }
  9.  
  10. close $a;
  11. close (Data);
  12.  
I am attaching the input and output files....
hi,
can u tell me how to attach a file.
Oct 23 '07 #8
Hi

if tried it on a couple of data sets but my output remains not properly outlined.

I attached my data files so you can look for yourself the output is misaligned
Attached Files
File Type: txt input.txt (180 Bytes, 422 views)
File Type: txt output.txt (266 Bytes, 371 views)
Oct 23 '07 #9
hi,
can u tell me how to attach a file.
You can attach a file when editing a post
Oct 23 '07 #10
numberwhun
3,509 Expert Mod 2GB
hi,
i worked with this code, it is working fine as like your required output.

Expand|Select|Wrap|Line Numbers
  1. open(my $a, '>File.txt');
  2.       $b ="0.5\n";
  3.       open (Data, 'Data.txt');
  4.       while ($c = <Data>){
  5.       chomp($c);
  6.       print $a "$c $b";
  7.       }
  8.       close $a;
  9.       close (Data);
  10.  
I had to edit your post to add the required code tags. Please be sure any time that you post code to the forum, that you surround it in the necessary code tags.

Regards,

Jeff
Oct 23 '07 #11

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

Similar topics

18
by: ZoombyWoof | last post by:
Hi. Im very new to php, and I have a problem here that I cant find the solution for. I have problems with session variables, I want some variables to maintain their value between different php...
7
by: Graham Taylor | last post by:
I've tried posting this in the 'microsoft.public.access' but I will post it here also, as I think it might be the webserver which is causing my problem. --------- I have an Access 2003 database...
6
by: David Nedrow | last post by:
I'm trying to set up the cyrus imap server using PostgreSQL as the authentication backend. Currently, the issue I'm having is that I'm unable to connect via psql on the loopback address...
1
by: sam.collett | last post by:
Is there a basic guide on Xml document creation and editing (simpler than the MSDN docs). Say I want to create a file containing the following: <?xml version="1.0" encoding="utf-8"...
2
by: NewBob | last post by:
Since Access automatically highlights all of the text in a text control (I use it to hold data from a memo field) when the control is activated, I've added the following code to put the cursor at...
1
by: E. Tom Jorgenson | last post by:
I've run into a problem on a couple of projects that I think I've identified - but would like confirmation of what I think is going on. Also interested in any fast solutions to fix the user...
4
by: Jeigh | last post by:
My host has been 'upgrading' lately and its caused me a whole mess of problems. The last of which being that my CAPTCHA form no longer works. I remember to get the CAPTCHA working it took me weeks to...
1
by: Sinan Alkan | last post by:
Hi All, I have a method to resize any image file to the specific dimensions and save this new image to a path. I found it on a web page(By Joel Neubeck) and i changed it for my project. The...
0
by: priyamtheone | last post by:
I'm trying to make a datagridview column to act like a datetimepicker column (C#.Net 2005). These are the behaviours that the dgv should have: 1) Initially all the cells of the dtp column should be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.