Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Y does it do this

Status
Not open for further replies.

skottieb

Programmer
Jun 26, 2002
118
AU
When I type in the following code does only one line from the file I open go into the $scalar, and the regular expression i've used allows numbers thru, Y and how do I fix it?? PLEASE HELP :(

#!/usr/bin/perl

open(CUST, "c:/customers.txt") || die "cannot open customers.txt\n";
$cust=<CUST>;
chomp $cust;
@cust=split(/ /, $cust);

@names=grep m![A-Za-z][^@][^\d]!, @cust;
print &quot;$cust&quot;;
 
This is a sample script of how I usually read a file.

open(CUST, &quot;c:\\customers.txt&quot;) || die &quot;Shucks $!&quot;;
@a = <CUST>;
close(CUST);

foreach $a (@a){
chomp($a)
print $a;
}


It looks like what you're doing is reading the file into a scaler instead of an array.
so $cust=<CUST>; only retains the last line read.

Hope this helps

tgus

____________________________________________________
A father's calling is eternal, and its importance transcends time.
 
You can also save a little time in your loop. If you're going to chomp the entire list anyway, you could do this, since chomp accepts list context as well as scalar:

open(CUST, &quot;c:\\customers.txt&quot;) || die &quot;Shucks $!&quot;;
@a = <CUST>;
chomp @a;
close(CUST);

foreach $a (@a){
print $a;
}
------------------------------------
As to the original poster's question on his regular expression, it would help if we knew the format of your customer file, and what exactly you are looking for. Your current regex looks for 1 alphabetic character, followed by 1 character that is anything except an @, followed by 1 character that is not a numeric digit. Is that what you're trying to do? Remember that a character class (something enclosed in [], only matches one character unless it's followd by a quantifier ( +?*{n,m} ).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top