473,651 Members | 3,024 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 2087
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
1820
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 keyfield), write it to a session variable, redirect to another form where I read the session variable and display the order detail. How do I read the OrderID value when the user clicks on a select button in a
3
7248
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
4901
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 = 2.5784852e-009 Double = 17.192157 When i use php to read file by code:
8
7254
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 retrieve. But, I am having problems actually reading the data for the number of Bytes that I need to get from each address. Here is some of the code I am currently working with: Dim fsread As FileStream = File.Open(OpenFileDialog1.FileName,...
4
6068
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 8.0;HDR=YES;IMEX=1;\""; OleDbConnection conn = new OleDbConnection(connection); try { DataSet ds = new DataSet(); conn.Open();
5
10836
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 value ). Can anyone please suggest me how can I achieve that in VB/C#.NET. Thanking you in advance. Regards
11
2651
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
1177
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 I am using code similar to the following:
0
1642
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 for passwords are administered by using "Local Security Settings" console and inside it with "Account Policies\Password Policy". What i need to do, is to read the value of "Maximum password age" by using c# code. I need to read the value of...
0
8278
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8807
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8584
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5615
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.