in reply to Creating an array from text in perl
Maybe you rather need a hash of arrays, HoA, using the names as the keys and arrays of numbers as values?
Output:#!/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
$VAR1 = { 'James' => [ '40', '35', '26', '15' ], 'Philip' => [ '12', '27' ] };
|
|---|
| 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 |