Help - Search - Member List - Calendar
Full Version: finding needle in a haystack
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Bret Goodfellow
All right, I know how to do this in REXX because there is a word()
function, but how do I do this in Perl. I want to read in one record at
a time, that has space-delimited fields. There may be multiple spaces
between the words. I want to be able to get for example, the 4th word
of the record. How do I do this? Just for grins, my record looks like
this:

/dev/sun11/lvol13 1032192 377921 613445 38% /techsupp

Chris Devers
On Wed, 22 Jun 2005, Bret Goodfellow wrote:

QUOTE
All right, I know how to do this in REXX because there is a word()
function, but how do I do this in Perl.

The split() function is what you're looking for.

$ perldoc -f split

The perldoc gives this example:

A pattern matching the null string (not to be confused with a
null pattern "//", which is just one member of the set of pat-
terns matching a null string) will split the value of EXPR into
separate characters at each point it matches that way. For
example:

print join(':', split(/ */, 'hi there'));

produces the output 'h:i:t:h:e:r:e'.

It shouldn't be hard to adapt this example to your needs.



--
Chris Devers

Chris Devers
On Wed, 22 Jun 2005, Ing. Branislav Gerzo wrote:

QUOTE
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:

No, not like that. A regex in a split will be easier.

my $fields = split( /s+/, $record );
my $fourth = $fields[3];

This problem doesn't require any looping! :-)



--
Chris Devers

Ing. Branislav Gerzo
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.]

Bret Goodfellow
-----Original Message-----
From: Chris Devers [mailto:[Email Removed]]
Sent: Wednesday, June 22, 2005 3:17 PM
To: Bret Goodfellow
Cc: Perl Beginners List
Subject: Re: finding needle in a haystack


On Wed, 22 Jun 2005, Bret Goodfellow wrote:

QUOTE
All right, I know how to do this in REXX because there is a word()
function, but how do I do this in Perl.

The split() function is what you're looking for.

$ perldoc -f split

The perldoc gives this example:

A pattern matching the null string (not to be confused with a
null pattern "//", which is just one member of the set of pat-
terns matching a null string) will split the value of EXPR into
separate characters at each point it matches that way. For
example:

print join(':', split(/ */, 'hi there'));

produces the output 'h:i:t:h:e:r:e'.

It shouldn't be hard to adapt this example to your needs.



--
Chris Devers

Thanks Chris,

This is what I actually did to resolve my issue:

@words = split(/ * /, $line)
Print "the third word is: $words[2]n";

Bret Goodfellow

Ing. Branislav Gerzo
Chris Devers [CD], on Wednesday, June 22, 2005 at 17:53 (-0400 (EDT))
wrote the following:

CD> my $fields = split( /s+/, $record );
CD> my $fourth = $fields[3];
CD> This problem doesn't require any looping! :-)

I, know, thats just example.

I read whole the message, in original was:
"I want to read in one record at a time, that has space-delimited
fields."

Also I think, author will need 2-nd, and n-th "word", so maybe he
found my example useful. Also perl is about
"not only 1 way is correct:)"...but we know that.

--

How do you protect mail on web? I use http://www.2pu.net

[Wish to our future on a far away star.]

Ing. Branislav Gerzo
Bret Goodfellow [BG], on Wednesday, June 22, 2005 at 15:55 (-0600)
wrote these comments:

QUOTE
This is what I actually did to resolve my issue:
@words = split(/ * /, $line)
Print "the third word is: $words[2]n";

this is not good, you don't know about regexpes much, eh?
first argument in split is //, where inside of those braces is regexp.
What you have will work, but it is possible, you have input
delimited with tabulators, and your example will not work. This is
better:
@words = split(/s+/, $line);

this say, $line will split into @words, where delimiter is one or more
white characters (including tabs ofcoz)

--

How do you protect mail on web? I use http://www.2pu.net

[An android chaperone? * K'Ehleyr]


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.