[comp.lang.perl] formatting DB records

srs@ticnj.UUCP (Scott Strool) (02/12/91)

I hope that someone can point me in the correct direction.

I have a file that is output from a database. The fields are '|' separted.

I want to write a script that will read the file and print the records.
I would like to break the line into fields, each field is the text between
the pipe characters. I then want to sort on the key fields and then print
the records without the pipe chars.

What Perl function/commands would I need to use.
I looked at split but could not get that to split my line of text into
the separate named fields. Here is my split line:

	($a,$b,$c) = split(/|/);
	print $a, $b, $c;

	... prints nothing.




-- 
Scott Strool      	(908) 747-4700		srs@iex.com	
IEX Corporation
125 Half Mile Rd.	
Red Bank, NJ 07701

srs@ticnj.UUCP (Scott Strool) (02/12/91)

After further experimentation I have found that the split function is
appropriate. The one problem that I have come across is the pattern
character. I was using the pipe char |. split seemed to ignore this,
actually it returned just one character of the field i.e if the field
was SCORE it returned SC. When I changed the delimeter in my file to a 
colon, the script worked as ecpected. 

What is wrong with the | character, does it have to be escaped?

-- 
Scott Strool      	(908) 747-4700		srs@iex.com	
IEX Corporation
125 Half Mile Rd.	
Red Bank, NJ 07701

tchrist@convex.COM (Tom Christiansen) (02/12/91)

From the keyboard of srs@ticnj.UUCP (Scott Strool):
:After further experimentation I have found that the split function is
:appropriate. The one problem that I have come across is the pattern
:character. I was using the pipe char |. split seemed to ignore this,
:actually it returned just one character of the field i.e if the field
:was SCORE it returned SC. When I changed the delimeter in my file to a 
:colon, the script worked as ecpected. 
:
:What is wrong with the | character, does it have to be escaped?

Sure enough -- perl uses egrep-style regexps, meaning that this like (,
|, and ) are not escaped for their special meanings.  In this case /|/
just said "match nothing or nothing".  You want /\|/ instead.

--tom
--
 "All things are possible, but not all expedient."  (in life, UNIX, and perl)