473,503 Members | 12,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read a series and add a value before and after that series.

1 New Member
I am very new to perl. Pls do help me out guys.

I have a SAMPLE.txt file with values
300000
295000
280000
300000
300000

I need to read this file and over-write or creat a new file as below

300000
295000
280000
300000

i.e. For values series other than 300000, add 300000 before and after the values as above.
May 22 '15 #1
4 2081
computerfox
276 Contributor
How's this?

Expand|Select|Wrap|Line Numbers
  1. #!/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. sub gen(){
  6.  my $path=shift;
  7.  my $handler;
  8.  my @rows;
  9.  open($handler,"<",$path);
  10.  while(<$handler>){
  11.   my $row=$_;
  12.   chomp($row);
  13.   push(@rows,$row);
  14.  }
  15.  for(my $i=0;$i<$#rows;$i++){
  16.   print $rows[$i]."\n";
  17.   if($i>2){
  18.    if($rows[$i] != "300000" && $rows[-2] != "300000"){
  19.     print "300000\n";
  20.    }
  21.   }
  22.  }
  23. }
  24. sub menu(){
  25.  print "perl gen30.pl {input_file}";
  26. }
  27. if($#ARGV>-1){
  28.  &gen($ARGV[0]);
  29. }
  30. else{
  31.  print "Please provide the path to the input file...";
  32.  print "\n";
  33.  &menu();
  34. }
  35.  
You would run the script like this:
Expand|Select|Wrap|Line Numbers
  1. perl gen30.pl sample.txt > output.txt
  2.  
http://safe.abelgancsos.com/codepost...ect.php?id=389
May 22 '15 #2
RonB
589 Recognized Expert Moderator Contributor
Your description of the desired output is a little vague.

Given this file content:
300000
295000
280000
300000
270000
300000
300000

Should the output be:
300000
295000
280000
300000
300000
270000
300000

or should it be:
300000
295000
280000
300000
270000
300000

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. local $/ = "300000\n";
  7.  
  8. while (<DATA>) {
  9.     chomp;
  10.     next if /^\s*$/;
  11.     print "$/$_$/";
  12. }
  13.  
  14. __DATA__
  15. 300000
  16. 295000
  17. 280000
  18. 300000
  19. 270000
  20. 300000
  21. 300000
  22.  
May 22 '15 #3
RonB
589 Recognized Expert Moderator Contributor
FYI,
Expand|Select|Wrap|Line Numbers
  1.   while(<$handler>){
  2.   my $row=$_;
  3.   chomp($row);
  4.   push(@rows,$row);
  5.  }
Could be shortened and better written as:
Expand|Select|Wrap|Line Numbers
  1. chomp(my @rows = <$handler>);
And:
Expand|Select|Wrap|Line Numbers
  1. for(my $i=0;$i<$#rows;$i++){
is better written as:
Expand|Select|Wrap|Line Numbers
  1. for my $i (0 .. $#rows - 1) {
May 22 '15 #4
computerfox
276 Contributor
Well noted Ron.
Thanks.
May 22 '15 #5

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

Similar topics

2
1813
by: mharness | last post by:
Hello, I'm new at this. Thanks for your help. I have form which displays a customer record and a datagrid of that customer's orders. I want to read the value of the OrderID (which is the...
3
7240
by: reneeccwest | last post by:
<input type="text" size="7" name="test" maxlength="4" value="4Tyourname"> Is there any way I can make first 2 characters as a read only value?
1
4883
by: nooy66 | last post by:
I need to read value type double form my binary file. When i use Hex Workshop software read my binary file. i have data 8 bytes : hex = 3131 3131 3131 3140 string = 1111111@ Float =...
8
7241
by: james | last post by:
I am trying to use Filestream to read a file ( .DAT) that contains values in HEX that I want to convert to text. I know the different offset addresses for each portion of the data I am trying to...
4
6047
by: Hardy Wang | last post by:
Hi all, I have following code to read Excel content into a DataSet string connection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=\"Excel...
5
10826
by: vighnesh | last post by:
Hi Folks Greetings to you all. I am dealing with a project ( windows application ) in which I have to read the REG_BINARY value from the Registry and get its string equalent ( the ASCII text...
11
2623
by: xz | last post by:
>From the reference of MSDN about hash map: operator Inserts an element into a hash_map with a specified key value. And such example is given: hash_map <int, inthm1; hm1 = 40;
1
1173
by: ma | last post by:
Hello, I have a gridview that is bound to a dataset. One col of this grid is bound to the primary key of the dataset which its data format is integer. I want to read the value of this key and...
0
1634
by: =?Utf-8?B?UmljYXJkbyBRdWludGFuaWxsYQ==?= | last post by:
I have an Adam (active directory application mode) instance on windows server 2003, and the server does not belong to a domain, i mean it is a standalone server. So in this context the policies...
0
7067
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
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
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...
1
6975
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
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.