in reply to Creating an array from text in perl

It's unclear what you're asking. It seems you want the numbers to become elements of the array, but what about James? Are there any other names in your input?

Maybe you rather need a hash of arrays, HoA, using the names as the keys and arrays of numbers as values?

#!/usr/bin/perl use warnings; use strict; my %hash; my $name; while (<DATA>) { chomp; if ($. % 2) { # Even lines. $name = $_; } else { push @{ $hash{$name} }, $_; } } use Data::Dumper; print Dumper \%hash; __DATA__ James 40 Philip 12 James 35 James 26 James 15 Philip 27
Output:
$VAR1 = { 'James' => [ '40', '35', '26', '15' ], 'Philip' => [ '12', '27' ] };
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Creating an array from text in perl
by tryingoutperl (Initiate) on May 27, 2021 at 19:43 UTC
    Yes that is the exact output I was looking for. Thanks for your time!