On Friday, September 27, 2002, at 12:50  am, Georgia Citizen wrote:

> I have a variable that contains multiple lines, each ending with a 
> line-feed.
>
> I want to create an array that contains one line per each line of text 
> contained in a variable.

As you don't say i don't know what your original data looks like but 
there's one thing to remember when dealing with docs from the web -'\n' 
is not the same for different OS:

Unix systems (Unix, Linux, OSX)  '\n' = ASCII '\012' ('\f' line feed).
MacOS9 and earlier                        '\n' = ASCII '\015' ('\r' 
carriage return).
MS-DOS,Windows systems           '\n' = ASCII '\012\015' ('\f\r' line 
feed + carriage return).

So if you're processing files which come from a Unix system, or 
Windows, unless you make sure the data has the correct endings first 
MacPerl won't be able to use the method below:

[1] $data=~tr/\012/\015/;          # change Unix line endings to Mac ones
or
[1] $data=~tr/\012//;               # change Dos/Win line endings to 
Mac ones

[2] $data='Tony\nJeff\nJimmy\nTammy\n';

[3] @array=split(/\n/,$data);   #split the data on on each \n

but you could also do this:

$data='Tony\n,Jeff\n,Jimmy\n,Tammy\n';
($Tony,$Jeff,$Jimmy,$Tammy ) =split(/\n/,$data);

Arrays are good for gathering data in one structure, especially if 
you're going to process the data later. Variables are good because you 
can give them meaningful names so:

$data='Tony\n,Jeff\n,Jimmy\n,Tammy\n';
@array=split(/\n/,$data);
print $array[0],"\n";

and

$data='Tony\n,Jeff\n,Jimmy\n,Tammy\n';
($Tony,$Jeff,$Jimmy,$Tammy ) =split(/\n/,$data);
print $Tony,"\n";

do the same thing, it depends what you want to do next.
HTH

Robin




Reply via email to