Bret Goodfellow [BG], on Wednesday, June 22, 2005 at 15:11 (-0600)
typed the following:

BG> All right, I know how to do this in REXX because there is a word()
BG> function, but how do I do this in Perl.  I want to read in one record at
BG> a time, that has space-delimited fields.  There may be multiple spaces
BG> between the words.  I want to be able to get for example, the 4th word
BG> of the record.  How do I do this?  Just for grins, my record looks like
BG> this:
 
BG> /dev/sun11/lvol13   1032192  377921  613445   38% /techsupp

use regular expressions, for example like this:

use strict;
use warnings;

my $string = '/dev/sun11/lvol13   1032192  377921  613445   38% /techsupp';

# for every word in $string
while ( $string =~ /(\S+\s+)/g ) {
        print $1, "\n";
}

#get 4th word in string
my ($out) = $string =~ /(?:\S+\s+){3}(\S+)/;
print $out;

__END__

\s - white character (spaces, tabs...)
\S - opposite of \s

-- 

 ...m8s, cu l8r, Brano.

[Confucious say:  Man who smoke pot choke on handle.]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to