in reply to Creating an array from text in perl

A little closer to production code by opening a "file" and checking for unexpected data:

use strict; use warnings; use Data::Dumper; my $testData = <<DATA; >James >40 >James >35 >James >26 >James >15 DATA open my $fin, '<', \$testData or die "Can't open file: $!\n"; my %people; my $lastPerson; while (defined (my $line = <$fin>)) { chomp $line; if ($line =~ /^>(\d+)$/) { die "Unexpected data order: '$line'\n" if !defined $lastPerson +; push @{$people{$lastPerson}}, $1; next; } if ($line =~ /^>(\w+)$/) { $lastPerson = $1; next; } warn "Don't know how to process '$line'\n"; } print Dumper(\%people);

Prints:

$VAR1 = { 'James' => [ '40', '35', '26', '15' ] };
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond