Perl question

Hello Everybody,

I am using perl.....
I have a variable called line which stores the line of a file.
I dont know how many words there will be in this line.
But I would like to find out in perl and also store these words in the $1,$2,$3 and $4 variables eg if the line is

"first second third forth....."

then i would like to store it as follows

$1= first
$2= second
$3= third
$4= forth

and so on

How can i do this in perl?

Many thanks

Rkap

ps. initial attempt

$_ = $line;
if (/\s*([a-zA-Z1-9':''.']+)\s+([a-zA-Z1-9':''.']+)\s+([a-zA-Z1-9':''.']+)/){
print "\$1 is $1\n";
print "\$2 is $2\n";
print "\$3 is $3\n";
}

but this assumes there will always be 3 words in the line which is not true

It's generally not in good taste to just ask for the answer, but to show or tell what you have done to try to solve the problem and then to ask for pointers. You have already done the first of these two things so I'll definitely give you a hint. In perl @arrays and %hashes as well as just about everything else is designed without limits. So if you are going to want to store an undefined number of elements into a usable variable, you might want to consider using a well designed &subroutine to break up the line into different elements and then store those elements into an @array. Then it will be fairly straight forward to pull those elements out and to store them as separate $string variables. There are of course many different ways to solve any one problem with perl, hence its beauty.

You might also want to try, either in lew of the afore-mentioned way or afterward just for fun, to incorporate a %hash instead. Something like
%word_on_line
where
$word_on_line{$1}="first"

see if this gets you any further I have some other ideas, but you should just see where this takes you for now. Make sure to post back what you some up with :slight_smile:

Why don't you use split()?