Samuel Hardman wrote:
Quote:
Hello,
>
I am trying to write a perl script to parse a string into an array. The
string has the fields separated by tabs. So what I want to do is read each
field into a variable so I can process the data further.
|
I suggest a simple regex. If you don't know about regexs yet, you're
going to be missing out on much of Perl's usefulness. There are several
very good texts available, including "Mastering Regular Expressions" by
Jeffrey Friedl. And of course, Perl's own documentation is more than
adequate learning for a beginner.
A regex to process your data could be as simple as:
my ($field_1, $field_2) = $line_of_text =~ /^(\S+)\s+(\S+)/;
Which would stuff the first two fields into the declared variables. A
while() loop can also be useful to match an arbitrary number of fields,
and push them onto a stack.
And of course there's always CPAN.