Here is another solution. There are probably a million ways to do this, so...

#!/usr/bin/perl -w use strict; use warnings; my $TEXT = <<'END'; >James >40 >James >4 >John >35 >James >26 >John >31 >James >15 >Pete END $TEXT =~ tr|a-zA-Z0-9||cd; # Remove everything except letters a +nd numbers my @ARRAY = SplitNumbers($TEXT); # Split numbers vs letters if (@ARRAY & 1) { pop(@ARRAY); } # Remove last element if it has no n +umber following it. my %HASH; # This is used as a global here for (my $i = 0; $i < @ARRAY; $i+=2) { if (exists($HASH{$ARRAY[$i]})) { $HASH{$ARRAY[$i]} .= ', '; } $HASH{$ARRAY[$i]} .= $ARRAY[$i+1]; } print GetList('James'); print GetList('John'); print GetList('Pete'); print GetList('Bob'); exit; ################################################## # Returns the named list in nicely formatted way. # # Usage: STRING = GetList(NAME) # sub GetList { @_ or return ''; my $NAME = shift; # Get first argument, which is the 'NAME' return "\n $NAME = [" . (exists($HASH{$NAME}) ? $HASH{$NAME} : '') + . "]\n"; } ################################################## # # Splits a string along numbers and returns an # array of alternating numbers and text. # Usage: ARRAY = SplitNumbers(STRING) # # Example: SplitNumbers('abc45tt-35203.19') -> ["abc", 45, "tt-", 3520 +3, ".", 19] # sub SplitNumbers { defined $_[0] or return (); my ($PTR, $PREV, $LEN, $TYPE, @A) = (0, -1, length($_[0])); $LEN or return (); # Possible values for $PREV: -1=Uninitialized 0=NUMBER 1=TEXT for (my $i = 0; $i < $LEN; $i++) { $TYPE = vec($_[0], $i, 8); $TYPE = $TYPE < 48 || $TYPE > 57; # Is it a number? if ($PREV == !$TYPE) # Same as before? { push(@A, substr($_[0], $PTR, $i-$PTR)); $PTR = $i; } $PREV = $TYPE; } push(@A, substr($_[0], $PTR)); # Process last chunk return @A; } ##################################################

This code outputs the following:

James = [40, 4, 26, 15] John = [35, 31] Pete = [] Bob = []

In reply to Re: Creating an array from text in perl by harangzsolt33
in thread Creating an array from text in perl by tryingoutperl

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.