####  Fill lines from a string into an array:

$_ = "Tony
Jeff
Jimmy
Tammy";

   ## TIMTOWToDI -- either (1.)
@kids = split "\n", $_;
     print "(1.) ", join ":", @kids;
   ## or, because both parameters are default:
@kids = split;
     print "\n(1a.) ", join ":", @kids;
     print "\n(1b.) @kids";

   ## or (2.)
@kids = m,(.+),g;
print "\n(2.) ", join ":", @kids;

   ## or (3.)
s'(.+)'push (@Kids, $1); $1'eg;
print "\n(3.) ", join ":", @Kids;

   ## or (4.)
s'(.+)'remember($1)'eg;
sub remember {
  push @KIds, $_[0] }
print "\n(4.) ", join ":", @KIds;

   ## finally (5.)
@kids = split "\n", $_;
     print "\n(5.) ", join ":", @kids;


=Comments:

TIMTOWToDI = There is more than one way to do it:

(1.) split is a good choice.
(2.) matching as well.
(3.) substituting can be very powerful,
because the right side of
      s'SearchPattern'EvaluateAndReplacePattern'eg;
by means of the switches
   e (evaluate the right side)
and
   g (global = again and again)
can have any Perl code, including
(4.) functions (subroutines);

In (3.) after grabbing the kid
I have to put it back into the row by means of  $1;
(in an evaluated substitution the last value is put back),
otherwise it would not be found in (4.).
In (5.) the kid is not found (only its count).


Reply via email to