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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple file and array operation problem

Status
Not open for further replies.

panuvin

Programmer
Mar 24, 2004
11
US
Hello,

I'm having a lot of trouble getting a program working. I'm trying to open a file, seperate it into an array of words.
I've opened the file and split it into an array of
words. Then I need to take out the punctuation and test to see if any of the words begin with '[' and end in ']'. I'm not sure how to do this with regular expressions. This is what I have so far:

########Code Begin
open($fh,$param) or die "Cannot open file";
$file=<$fh>;
chomp $file;
@words=split(/ /,$file);
$size=@words;
########Code End

I'm then trying to cycle through the array and pull
the punctuation or test for a beginning/ending
character of '[' and ']'

It's not working...and I can't figure out why

########Code Begin
for($ct=0;$ct<$size;$ct++)
{
$theword=@words[$ct];
$_ = $theword;
if (/.,;!?$/)
{
print "test";
}
#######Code End

What am I doing wrong?
Using the regular expression on the $_ variable, it
should notice these punctuation marks at the end of
the word. Also, I'm wondering why I can't access the
first element of a scalar.....like the first
letter....or for this program, the first and last
being brackets.

Please help,

Very Confused,

Justin
 
your question is not very clear, it might help to post some sample lines of text you are working with. If your files are one liners or you only want to read the first line of a file this will work OK:

$file=<$fh>;

because that only reads the first line of the file.

your loop is poorly constructed:

Code:
for($ct=0;$ct<$size;$ct++)
{
  $theword=@words[$ct];
  $_ = $theword;
  if (/.,;!?$/)
  {
  print "test";
  }
}

should maybe be more like this:

Code:
foreach(@words) {
  if (/[.,;!?]$/) {#use a character class [.,;!?]
      print "Has punctuation: .,;!? at the end\n";
  }
  if (/^\[/ || /\]$/) { 
      print "Has a [] at the beginning or end\n";  
}
 
correcting a typo in the last code block above:

Code:
foreach(@words) {
  if (/[.,;!?]$/) {#use a character class [.,;!?]
      print "Has punctuation: .,;!? at the end\n";
  }
  if (/^\[/ || /\]$/) {
      print "Has a [] at the beginning or end\n";  
  }# <-- ending bracket was ommited
}
 
I don't know if this helps you, but Perl has a built-in character class [:punct:] that matches anything except letters, digits, control characters, or space characters. s/[[:punct:]]//g will remove all "punctuation" chars from a string. Note, however, that this will remove [ and ] also, so if it's crucial to keep these, maybe this is not the way to go. Just thought I'd mention it in case ...


 
Good point, I tend to forget POSIX supported syntax. But using tr/// should still be faster and a better habit to get into for just trading/removing characters when no further pattern matching is required.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top