[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Embedded Control Chars (fwd)



According to Mark F. Murphy:
> 
> I think the original point was that Perl scripts did not have embedded
> chars except for the newlines.  Every thing else is escaped out in the
> script.

Yep.  But the message to me was from someone else (Andy I
believe?) and it was talking about the more generic
question of why the routine Chris (I think?) posted might
have problems.  Or, in other words, why would you ever
embed control characters into a program.

PACK versus SPLIT:

PACK works very nicely if, and only if, you do not want to
embed multiple arrays into a single record.  Especially if
these multiple arrays are variable in length.  Let's say
that you have an array X.  X sometimes will be five entries
long.  At other times it might be as long as 200 entries.
If you try to do this via the PACk command you will have to
keep track of how long the array is and you would probably
have to modify the control string in some way, shape, or
form.  But if you use the SPLIT & JOIN commands - it's very
easy to create a record.  All you have to do is:

	$theRecord = join( "\001", @X );

and to split it back apart you just:

	@X = split( /\001/, $theRecord );

Ok, but now you've gone into the area where Chris' program
can mess you up because you are using control characters to
denote a separator.  I have a database at work (and another
at home) which has multiple arrays joined together.  These
arrays contain variable length information.  In addition to
this, I also have many different single instance variables
which must also be stored into the database.  The one at
work has around fifty singles and only three variable
lengths.  The one at home has only about twenty singles but
has about fifteen variable length arrays.  So what I do is
the following:

	@theRecord = ();
	$theRecord[++$#theRecord] = $theName;
	$theRecord[++$#theRecord] = $theCompany;
	.
	.
	.
	$theRecord[++$#theRecord] = join( "\002", @theDescription );
	.
	.
	.
	$theLine = join( "\001", @theRecord );

Then I just do a stat on the file to find the end of the
file, do a LENGTH on $theLine, and send it out to the
file.  This allows me to store arrays of any length into
the database.  Further, I can even have multiple dimensions
on the array by just going to \003, \004, etc....  So
@theDescription could contain subfields if I, somewhere
else, say:

	$theDescription[x] = join( "\003", @myInfo );

To retrieve all of this information I just do this:

	@theRecord = split( /\001/, $theLine );
	@theDescription = split( /\002/, $theRecord[#] );

Very simple and straight forwards.  :-)


***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch