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:
n00b question re: string matching/substitution across files

 



zoroastr
New User

Oct 23, 2008, 8:21 PM

Post #1 of 4 (1428 views)
n00b question re: string matching/substitution across files Can't Post

ok...i think this can be done, and i think i have the first part, but it might be more complicated than i realize...oh, and i'm a total n00b.

here goes...

i have a text file with IP addresses left-justified, like so:

???.???.???.?? some other info...
???.???.???.?? some other info...
???.???.???.?? some other info...
???.???.???.?? some other info...

...etc...

This list expands over time. My goal is to extract these IP strings and try to match them in another, differently-formatted text file and do a string replacement in that second file. In this case, the replacement is just appending a 4-character prefix to each.

So far, my code is grabbing the IP strings just fine, but I'm stuck after that. Here's what I have so far. The source file, containing the IP strings that I'm going to look for in the other file is called "list." I'm also printing the IP string array, just to make sure it's grabbing the right strings. So, at this point, I need to open the other file (prolly using the diamond operator), find the array elements, then do the swap...

#!/usr/bin/perl
use strict;
use warnings;

my @lines;
my $string;

# get lines from list

open(FILE1,"list") || die "Error: $!\n";
@lines = <FILE1>;

foreach $string(@lines) {
$string=substr($string, 0, 11);
}

print @lines;


----------thanks, gurus.


(This post was edited by zoroastr on Oct 23, 2008, 8:22 PM)


KevinR
Veteran


Oct 23, 2008, 8:56 PM

Post #2 of 4 (1423 views)
Re: [zoroastr] n00b question re: string matching/substitution across files [In reply to] Can't Post

You seem to have the general idea correct. If you need to edit the second file inplace you should look into the Tie::File module that comes with perl. It allows you to make edits to a file just like you would to an array.

The problem here is not knowing the contents of the second file. Do you have to search every line of the second file against every line in the array to find the stuff you want to change in the second file? If so, you open the second file (use Tie::File) and read in each line in succession and loop through the other array and run some type of comparison to find whatever it is you are looking for and do whatever this replacement is you mentioned. If there can only be one match per line of the second file you stop the looping of the array, go to the next line of the file and start the loop of the array again. You do this unitl you have read all the lines of the second file.
-------------------------------------------------


(This post was edited by KevinR on Oct 23, 2008, 8:56 PM)


zoroastr
New User

Oct 24, 2008, 1:45 PM

Post #3 of 4 (1418 views)
Re: [KevinR] n00b question re: string matching/substitution across files [In reply to] Can't Post

Hey..thanks, Kevin. Your approach sounds promising, and I'll start reading about your idea immediately. I just started going through the llama book last week, so your hint prolly refers to something I haven't yet encountered. The kewl thing is, the code itself doesn't know I'm a n00b, and will execute regardles...:)
thx


KevinR
Veteran


Oct 24, 2008, 1:59 PM

Post #4 of 4 (1415 views)
Re: [zoroastr] n00b question re: string matching/substitution across files [In reply to] Can't Post

Post back when you have more questions, always glad to help noobs that are willing to learn.
-------------------------------------------------

 
 


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

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