CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Adding Prefixes.

 



Ndx
Novice

Jul 28, 2009, 9:38 AM

Post #1 of 22 (2492 views)
Adding Prefixes. Can't Post

Hello,

I'm trying to make a small script that will edit a text file.

1st - It will search and replace some text - I was able to get it working thx to this site and google.

2nd It will add prefix for each line. ( I'm not sure how to specify lines ie: 1 Line prefix will be "XB", Line 2 prefix "WW" etc.

Is there any easy way to do it ?

Before:

Code
Perl Noobie 
Perl Master
Perl Who


After:

Code
WB Perl Noobie 
XX Perl Master
TT Perl Who



I would appreciate some help or some sample code...

Thank You!


(This post was edited by Ndx on Jul 28, 2009, 9:39 AM)


FishMonger
Veteran / Moderator

Jul 28, 2009, 9:49 AM

Post #2 of 22 (2487 views)
Re: [Ndx] Adding Prefixes. [In reply to] Can't Post

Create an array or a hash that maps the line number to the prefix, then use the line number to look up the prefix value in the array or hash.

Since you're needing sequential numbering for the indexing (i.e., the line numbers), an array would be the normal choice to hold the prefix data. But you need to remember that arrary's are 0 indexed, so you'll need to account for that when accessing the array element.


johnpc831
Novice

Jul 28, 2009, 9:50 AM

Post #3 of 22 (2486 views)
Re: [Ndx] Adding Prefixes. [In reply to] Can't Post

The way I usually do stuff like this is to read in the whole file, make changes, and then write everything back to the file.


Code
open(FH,"myfile.txt"); 
my @lines = <FH>;
close(FH);

my $prefix = 'TT'; # Or whatever your prefix is supposed to be

open(FH,">myfile.txt");
foreach(@lines){
print FH "$prefix $_";
}
close(FH);



Ndx
Novice

Jul 28, 2009, 10:00 AM

Post #4 of 22 (2484 views)
Re: [johnpc831] Adding Prefixes. [In reply to] Can't Post


In Reply To
The way I usually do stuff like this is to read in the whole file, make changes, and then write everything back to the file.


Code
open(FH,"myfile.txt"); 
my @lines = <FH>;
close(FH);

my $prefix = 'TT'; # Or whatever your prefix is supposed to be

open(FH,">myfile.txt");
foreach(@lines){
print FH "$prefix $_";
}
close(FH);


'
Thank You
This works great ... But it adds same prefix for all lines ..
I'm trying to have different prefix for each line ...




Code
#!/usr/bin/perl 

{
local $^I = "data2.bak"; # the backup extension
local @ARGV = "data.txt"; # the file to edit
while (<>) {
next if $. == 10; # this will skip line 10
print "Test1!" if $. == 1; # this in line 3
print; # this prints the line itself
print "Test2!" if $. == 8; # this in line 6
}
}


I found this code in FAQ ..

Anyways :) Thank you very much guys :)


KevinR
Veteran


Jul 28, 2009, 10:35 AM

Post #5 of 22 (2481 views)
Re: [Ndx] Adding Prefixes. [In reply to] Can't Post


Code
#!/usr/bin/perl  

{
local $^I = "data2.bak"; # the backup extension
local @ARGV = "data.txt"; # the file to edit
while (<>) {
next if $. == 10; # this will skip line 10
print "Test1!" if $. == 1; # this in line 3
print; # this prints the line itself
print "Test2!" if $. == 8; # this in line 6
}
}


That is probably the way I would do it to having a preference for perls built-in editor over other possibilities. Maybe your example code was wrong, but you have:

print "Test1!" if $. == 1; # this in line 3
print "Test2!" if $. == 8; # this in line 6


clearly if $. == 1 then that is line one and same for $. == 8, that is line eight.
-------------------------------------------------


FishMonger
Veteran / Moderator

Jul 28, 2009, 10:54 AM

Post #6 of 22 (2478 views)
Re: [Ndx] Adding Prefixes. [In reply to] Can't Post

If you're not adding a prefix to each line, then I'd use a hash and do this.


Code
#!/usr/bin/perl   

use strict;
use warnings;

my %prefix = ( 1 => 'Test1!',
8 => 'Test2!',
}

local $^I = "data2.bak"; # the backup extension
local @ARGV = "data.txt"; # the file to edit
while (<>) {
next if $. == 10; # this will skip line 10

print $prefix{$.} if exits $prefix{$.};

print; # this prints the line itself
}



KevinR
Veteran


Jul 28, 2009, 11:01 AM

Post #7 of 22 (2475 views)
Re: [FishMonger] Adding Prefixes. [In reply to] Can't Post


In Reply To
If you're not adding a prefix to each line, then I'd use a hash and do this.


Code
#!/usr/bin/perl   

use strict;
use warnings;

my %prefix = ( 1 => 'Test1!',
8 => 'Test2!',
}

local $^I = "data2.bak"; # the backup extension
local @ARGV = "data.txt"; # the file to edit
while (<>) {
next if $. == 10; # this will skip line 10

print $prefix{$.} if exits $prefix{$.};

print; # this prints the line itself
}



The hash is a good suggestion but I think the code is not quite right:

This line:


Code
    print $prefix{$.} if exits $prefix{$.};


Should be:


Code
    print "$prefix{$.} $_" if exists $prefix{$.};


Fish, you had "exits" instead of "exists" which is obviously just a typo but I bring it to your attention.
-------------------------------------------------


Ndx
Novice

Jul 28, 2009, 11:17 AM

Post #8 of 22 (2473 views)
Re: [KevinR] Adding Prefixes. [In reply to] Can't Post

Hi Thank You ...

When I'm trying to execute this I'm getting ...

admin$ perl x.pl
syntax error at x.pl line 10, near "local"
Execution of x.pl aborted due to compilation errors.

I can't see anything wrong there


Code
#!/usr/bin/perl    

use strict;
use warnings;

my %prefix = { 1 => 'Test1!',
8 => 'Test2!',
}

local $^I = "data2.txt"; # the backup extension
local @ARGV = "data.txt"; # the file to edit
while (<>) {
next if $. == 10; # this will skip line 10

print "$prefix{$.} $_" if exists $prefix{$.};

print; # this prints the line itself
}



And this ... Prints to 1st line .. then stops ... Don't know why :(



Code
#!/usr/bin/perl  

{
local $^I = "data2.txt"; # the backup extension
local @ARGV = "data.txt"; # the file to edit
while (<>) {
print "Powered " if $. == 1;
print "Test " if $. == 2;
print "Energy " if $. == 3;
print "Voltage " if $. == 4;
print "Watts " if $. == 5;
print "Output " if $. == 6;
print "Nano " if $. == 7;
print "Particles " if $. == 8;
print "Uoa " if $. == 9;
print "Version " if $. == 10;
print "Test " if $. == 11;
print "EU " if $. == 12;
print "V2 " if $. == 13;
print "V3 " if $. == 14;
print "V1 " if $. == 15;
print;

}
}



(This post was edited by Ndx on Jul 28, 2009, 11:20 AM)


KevinR
Veteran


Jul 28, 2009, 11:24 AM

Post #9 of 22 (2468 views)
Re: [Ndx] Adding Prefixes. [In reply to] Can't Post

Looks like our friend Fish was posting too quickly, there is a mistake here:


Code
my %prefix = ( 1 => 'Test1!',   
8 => 'Test2!',
}


should be:


Code
my %prefix = ( 1 => 'Test1!',   
8 => 'Test2!',
);


Or maybe he didn't get his cup of java today Wink
-------------------------------------------------


FishMonger
Veteran / Moderator

Jul 28, 2009, 11:28 AM

Post #10 of 22 (2467 views)
Re: [Ndx] Adding Prefixes. [In reply to] Can't Post

Oops,

Code
my %prefix = { 1 => 'Test1!',   
8 => 'Test2!',
}


Should have been

Code
my %prefix = { 1 => 'Test1!',   
8 => 'Test2!',
);



KevinR
Veteran


Jul 28, 2009, 11:28 AM

Post #11 of 22 (2467 views)
Re: [Ndx] Adding Prefixes. [In reply to] Can't Post


Quote
And this ... Prints to 1st line .. then stops ... Don't know why :(


I don't see why it would stop at the first line. But that is clearly not a good way to go about your task. You are forcing the script to evaluate each "if" condition unnecessarily. Use the hash or use an if/elsif/elsif/...../else block.
-------------------------------------------------


KevinR
Veteran


Jul 28, 2009, 11:29 AM

Post #12 of 22 (2464 views)
Re: [FishMonger] Adding Prefixes. [In reply to] Can't Post


In Reply To
Oops,

Code
my %prefix = { 1 => 'Test1!',   
8 => 'Test2!',
}


Should have been

Code
my %prefix = { 1 => 'Test1!',   
8 => 'Test2!',
);



Fish, its still wrong. Unsure
-------------------------------------------------


FishMonger
Veteran / Moderator

Jul 28, 2009, 11:30 AM

Post #13 of 22 (2463 views)
Re: [KevinR] Adding Prefixes. [In reply to] Can't Post

I definitely would not use the if/elsif/elsif approach. That would get very messy.

Use an array or a hash.


KevinR
Veteran


Jul 28, 2009, 11:33 AM

Post #14 of 22 (2461 views)
Re: [FishMonger] Adding Prefixes. [In reply to] Can't Post


In Reply To
I definitely would not use the if/elsif/elsif approach. That would get very messy.


Me either, but I sure wouldn't use if/if/if which is just inefficient since the condition can only ever be true once per line of the file. The hash looks to be the way to go.
-------------------------------------------------


FishMonger
Veteran / Moderator

Jul 28, 2009, 11:36 AM

Post #15 of 22 (2460 views)
Re: [KevinR] Adding Prefixes. [In reply to] Can't Post


In Reply To
Fish, its still wrong. Unsure


I must be real tired, I copy/past the wrong one. Blush

Code
my %prefix = ( 1 => 'Test1!',    
8 => 'Test2!',
);



FishMonger
Veteran / Moderator

Jul 28, 2009, 11:38 AM

Post #16 of 22 (2459 views)
Re: [KevinR] Adding Prefixes. [In reply to] Can't Post


Quote
The hash looks to be the way to go.


The has is IMO more readable, but the array would be the most efficient.


KevinR
Veteran


Jul 28, 2009, 11:42 AM

Post #17 of 22 (2455 views)
Re: [FishMonger] Adding Prefixes. [In reply to] Can't Post


In Reply To

Quote
The hash looks to be the way to go.


The has is IMO more readable, but the array would be the most efficient.


Yea, after looking back at it, since there are all sequential it should be the best way.
-------------------------------------------------


Ndx
Novice

Jul 28, 2009, 11:42 AM

Post #18 of 22 (2452 views)
Re: [KevinR] Adding Prefixes. [In reply to] Can't Post

Thank You Guys,

I'm crazzy or something is wrong ...


Code
#!/usr/bin/perl    

use strict;
use warnings;

my %prefix = ( 1 => 'Test2!',
2 => 'Test3!',
3 => 'Test4!',
4 => 'Test5!',
);

local $^I = "data2.bak"; # the backup extension
local @ARGV = "data.txt"; # the file to edit
while (<>) {

print "$prefix{$.} $_" if exists $prefix{$.};
}


Again it prints only in 1st line then stops.

data.txt
@ NanoVolt-Uno-Quad-Small: 324786 +345
@ NanoVolt-Uno-Quad-Small: 35436 +545
@ NanoVolt-Uno-Quad-Small: 8888.51 +145
@ NanoVolt-Uno-Quad-Small: 56445 +54
@ NanoVolt-Uno-Quad-Small: 56435 +1657

Whats funny ... If I create a file called a new data.txt Inside..

28347298 893729873
87236478 783624876
78236476 738264678
32874888 283976489

It prints perfectly fine to this file!


(This post was edited by Ndx on Jul 28, 2009, 11:46 AM)


Ndx
Novice

Jul 28, 2009, 12:00 PM

Post #19 of 22 (2442 views)
Re: [Ndx] Adding Prefixes. [In reply to] Can't Post

O I know whats wrong ... Don't know how to fix it lol!

Original "data.txt"
When I open it with txt editor looks fine:
@ NanoVolt-Uno-Quad-Small: 324786 +345
@ NanoVolt-Uno-Quad-Small: 35436 +545
@ NanoVolt-Uno-Quad-Small: 8888.51 +145
@ NanoVolt-Uno-Quad-Small: 56445 +54

But when I open it with VI editor I'm getting:

@ NanoVolt-Uno-Quad-Small: 324786 +345^M@ NanoVolt-Uno-Quad-Small: 35436 +545^M@ NanoVolt-Uno-Quad-Small: 8888.51 +145^M@ NanoVolt-Uno-Quad-Small: 56445 +54^M

So it stops at 1st line because it looks like there is no more lines ...

what ^M stands for ? Is there a easy way to parase it .. ?


Ndx
Novice

Jul 28, 2009, 1:12 PM

Post #20 of 22 (2437 views)
Re: [Ndx] Adding Prefixes. [In reply to] Can't Post

solution:

perl -pi -e 's|\r|\n|g' data.txt


KevinR
Veteran


Jul 28, 2009, 1:21 PM

Post #21 of 22 (2436 views)
Re: [Ndx] Adding Prefixes. [In reply to] Can't Post

Files was created on a Mac? Windows would be \r\n and Mac should just be \r (or ^M). Unix is \n.
-------------------------------------------------


Ndx
Novice

Jul 28, 2009, 1:22 PM

Post #22 of 22 (2435 views)
Re: [KevinR] Adding Prefixes. [In reply to] Can't Post


In Reply To
Files was created on a Mac? Windows would be \r\n and Mac should just be \r (or ^M). Unix is \n.


Mac, Thank You good to know

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives