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

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

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 2075
computerfox
276 100+
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 Expert Mod 512MB
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 Expert Mod 512MB
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 100+
Well noted Ron.
Thanks.
May 22 '15 #5

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

Similar topics

2
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
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
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
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
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
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
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
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
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.