in reply to getting next word or number after another

Below is an obvious solution to your current formulation of the problem. You want: first word, first number, second word, second number. So make a stack of the words and a stack of the numbers and then interleave them for printout. I suspect that you were closer to a flexible solution when you had an array of lines. BTW, I do not consider parsing $string twice to be a problem - this makes the code easier and it will run so fast that it won't matter.

When you keep reformulating the problem without showing edits and starting new threads, this just confuses the issue. It makes it very tough for a guy like me who just stumbles across this thing to make heads or tails of it.

use strict; use warnings; my $string = " info John 100 - 2000 Kent"; (my @words) = $string =~ /([A-Za-z]+)/g; (my @nums) = $string =~ /(\d+)/g; print "data:",shift @words," "; #the "info" word print "uneven stack error!" if (@words != @nums); while (@words) { print "",shift @words,":",shift @nums," "; } print "\n"; # prints: "data:info John:100 Kent:2000 " <= what you said you wanted
Update: When you say things like "now my problem i cant get the next word or number after john", we've lost all context about what the overall objective is because one would have to read another thread(s) to find out what the end result is that you desire. So, the responses become focused upon answering your current question which is not really what you need to know or even pertinent to what asked for in the first place!