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

I am a Perl beginner!! 1

Status
Not open for further replies.

AliFessi

Programmer
Jul 3, 2001
12
DE
Hi there,

I am a Perl beginner. I am writing a Perl programm to parse some data. This is a little example to show you how the data looks like:

*****************
52 + 645 s 76
21 + 54 l 5 s 87
21 - Lib tmp 5446
*****************

My code looks like that:

##################
while(<FILE>)
{
@buf = split(/ /,$_);
if($buf[1] == &quot;+&quot;)
{
# ....
}
else
{
# ....
}
}
close(FILE);
####################

The result of &quot;if-statement&quot; is always &quot;false&quot;!! Can you explain me how to realize this distinction of cases?

An other question: the first argument of split should be a character. Is there a possibility how to separate the buffer elements with a set of characater, for example both space and tab?

I would be very thankful for your help.
 
The answer to the first question is that you are using the wrong comparison operator. You are using the number equality operator '==' on a string. The string equality operator 'eq' should be used. Change the 'if' to:
Code:
if($buf[1] eq &quot;+&quot;)

The split function can use any number of character or regular expression operators. To split on a space and a tab:
Code:
@buf = split(/\s\t/,$_);
If you want to split on whitespace or a tab:
Code:
@buf = split(/\s+|\t/,$_);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top