Home: Perl Programming Help: Frequently Asked Questions:
How do I process each word on each line?



Jasmine
Administrator

Mar 15, 2001, 6:04 AM


Views: 36338
How do I process each word on each line?

How do I process each word on each line?

Use the split function:


Code
    while (<>) { 
foreach $word ( split ) {
# do something with $word here
}
}

Note that this isn't really a word in the English sense; it's just chunks of consecutive non-whitespace characters.

To work with only alphanumeric sequences, you might consider


Code
    while (<>) { 
foreach $word (m/(\w+)/g) {
# do something with $word here
}
}