Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Creating an array from text in perl

by GrandFather (Saint)
on May 27, 2021 at 23:42 UTC ( #11133188=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11133188]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2021-10-18 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (73 votes). Check out past polls.

    Notices?