Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Regular Expressions replacements

3 views
Skip to first unread message

jhgaylor

unread,
Sep 23, 2008, 2:44:18 PM9/23/08
to
So, I am working on a program to take a list of First and Last names
exported from Novell Groupwise and making the list become Last, First,
firstfourlettersofLastName, password ||

The script I am using is this

#!"\xampplite\perl\bin\perl.exe"
use CGI qw/:standard/;
use overload;

$| = 1;
$q = new CGI;
print $q->header();

local $/=undef;
open FILE, "users.list" or die "Couldn't open file: $!";
binmode FILE;
$string = <FILE>;
close FILE;

$_ = $string;
chomp($_);
#Removes some of the formatting
s/Cof....Members//g;
s/To:\s//g;
s/Bc:.*\s//g;
#Checks if the expression will replace anything
if(m/ {1}\w.+\w\b.\w.+\w {1}/g){print "Hello";}
#Finds all names and replaces them with ___
s/ {1}\w.+\w\b.\w.+\w {1}/___/g;


open (OUT,">>usersFinal.list") || die;
print OUT $_;
close OUT;

The line I am having problems with is
s/ {1}\w.+\w\b.\w.+\w {1}/___/g;
The line is supposed to find all the names and remove them. It does
that, but replaces all the names with one instance of ___ instead of
___ for each name on the list. I can not break the names up by
looking for \n or \r. I am not sure why, but I think that they got
stripped

Eventually I want to add to the end of the name some other
information, but for now I just need to find the expression that finds
the names.

Below is an example of the starting information.

Cof2001Members
To: LastName1, FirstName1
To: LastName2, FirstName2
To: LastName3, FirstName3
To: LastName4, FirstName4
Bc: Zzip

Here is the way I want to eventually make the information look

Cof2001Members
Smith, Bob, smit001,
Jones, John, jone001,
Green, Jane, gree001,
Henry, Chris, henr001,


Either the expression to match the names and/or the code to add to the
end of names the needed information would be wonderful. Thanks
everyone.

Jim Gibson

unread,
Sep 23, 2008, 4:59:54 PM9/23/08
to
In article
<511d79ca-b236-43ce...@y38g2000hsy.googlegroups.com>,
jhgaylor <jhga...@gmail.com> wrote:

> So, I am working on a program to take a list of First and Last names
> exported from Novell Groupwise and making the list become Last, First,
> firstfourlettersofLastName, password ||
>
> The script I am using is this
>
> #!"\xampplite\perl\bin\perl.exe"
> use CGI qw/:standard/;
> use overload;

You should have here:

use strict;
use warnings;

You seem to be making it hard on yourself by trying to process the file
in one go. You are also discarding the 'To: ' string that marks the
relevant lines. Why don't you process the file line by line? Something
like this:

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

while(<DATA>) {
if( m{ To: \s+ (\w+) , \s+ (\w+) }x ) {
my( $last, $first ) = ($1, $2);
my $pwd = lc(substr($last,0,4)) . '001';
print "$last, $first, $pwd\n";
}
}

__DATA__
Cof2001Members
To: Smith, Bob
To: Jones, John
To: Green, Jane
To: Henry, Chris
Bc: Zzip

--
Jim Gibson

Ilya Zakharevich

unread,
Sep 24, 2008, 6:14:54 PM9/24/08
to
[A complimentary Cc of this posting was sent to
jhgaylor
<jhga...@gmail.com>], who wrote in article <511d79ca-b236-43ce...@y38g2000hsy.googlegroups.com>:
> #!"\xampplite\perl\bin\perl.exe"
> use CGI qw/:standard/;
> use overload;

Do not.

Hope this helps,
Ilya

jhgaylor

unread,
Sep 25, 2008, 3:16:14 PM9/25/08
to
Thanks Jim. I was actually thinking about doing this line by line,
but
I had no idea how to go through line by line. Looks like it is
working now.

I am using the code

$userList = "usersFinal.list";
open(userList) or die("Could not open the user list.");
foreach $line (<userList>) {
chomp($line); # remove the newline from $line.
print "${line}<br />";
# do line-by-line processing.
}

Jim Gibson

unread,
Sep 25, 2008, 8:55:38 PM9/25/08
to
In article
<d6561fd6-33b8-4d90...@8g2000hse.googlegroups.com>,
jhgaylor <jhga...@gmail.com> wrote:

Using a foreach loop instead of a while loop means that the entire file
is read into memory and $line is aliased to each line read one-by-one.
A while loop will only read and process one line at a time, so is more
efficient in its use of memory:

while( my $line = <userList> ) {
chomp($line);
#do line-by-line processing
}

--
Jim Gibson

0 new messages