Doug Essinger-Hileman wrote:
> I am just learning Perl, and am having a problem with something which
> seems like it should be so easy. Still . . . . I have read through a
> couple of books, including _Beginning Perl_ and _Picking Up Perl_, to
> no avail.

Welcome!

> 
> I am trying to read a file, then assign some information within a
> script. The problem comes in assigning. My file has three lines. The
> first line contains a list of names seperated by spaces; the next two
> lines contain numbers:
> 
> Doug Sandy Lois
> 0
> 1
> 
> In order to isolate the problem, I have created a simplified script:
> 
> #read from file
> 
> open (CONTROL1, "<test.cont");
> @constants = <CONTROL1>;
> close (CONTROL1);
> 
> #parse
> 
> $names = @constants[0];
> @group = qw($names);
> 
> #print
> 
> open (CONTROL2, ">test2.cont");
> print CONTROL2 "Names: $names";
> print CONTROL2 "Group: @group";
> close (CONTROL2);
> 
> The test2.cont file shows that $names is being set as I expected: to
> a string (Doug Sandy Lois). I assumed that @group = qw($names) would
> fill the array with the string of names. However, test2.cont shows
> that the value of @group is "$names". Obviously, my assumption was
> wrong.
> 
> So is there a way to directly fill the @group array with the string
> now stored in $names? Or do I have to split the string and fill the
> array in that manner?

qw() means "quote words". It's a shortcut for specifying a list of
LITERAL strings without having to use quotes and commas.

What you want is:

   @group = split ' ', $names;

qw() is documented in perldoc perlop. For the documentation on split(),
see perldoc -f split

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to