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!

In reply to Re: getting next word or number after another by Marshall
in thread getting next word or number after another by bigup401

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.