Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How to reference to array keys?

by Cristoforo (Deacon)
on Jul 27, 2011 at 01:40 UTC ( #916887=note: print w/ replies, xml ) Need Help??


in reply to How to reference to array keys?

The blank lines between records aren't blank - they contain spaces. If the data comes this way, I wouldn't be able to use the parsing method below. I had to delete all the spaces in the blank lines so they really were blank lines and then used the paragraph mode to read in 1 record at a time.

Anyhow, this is how I might parse the data (into the %data hash). Assumes RECORD_ID is unique for every set of owners and waiters.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %data; <DATA> for 1 .. 10; # toss out header { local $/ = ""; # paragraph mode while (<DATA>) { last if /^:/; my $owner; if (m{^(/.+)$}mg) { $owner = $1; } my @owner_rec = unpack "A13A13A2A10A8A5A8A9A*", $owner; $data{ $owner_rec[1] }{ owner } = \@owner_rec; my @waiter; while (m{^(/.+)$}mg) { my $waiter = $1; push @waiter, [ unpack "A13A13A2A10A8A5A8A9A*", $waiter ]; } $data{ $owner_rec[1] }{ waiter } = \@waiter; } } print Dumper \%data; __DATA__ UniData Release 7.2 Build: (3786) (c) Copyright Rocket Software, Inc. 1988-2009. All rights reserved. Current UniData home is /usr/udthome/. Current working directory is /usr/local/rfs/udt. :TERM ,0 :UDT.OPTIONS 20 ON :LOGTO /ud/JWP :LIST.QUEUE FILENAME RECORD_ID M OWNER UNBR UNO TTY TIME DA +TE /prod-data/J 00151120273 X jmorg 3584038 247 ts/49 13:38:12 Ju +l 20 ---------------------------------------------------------------------- +---- FILENAME RECORD_ID M WAITING UNBR UNO TTY TIME DA +TE /prod-data/J 00151120273 X jmorg 2015244 134 s/109 13:48:32 Ju +l 20 /prod-data/J 00151120273 X gdavi 1359996 62 ts/20 13:54:22 Ju +l 20 FILENAME RECORD_ID M OWNER UNBR UNO TTY TIME DA +TE /prod-data/J 001!L!311895 X jmorg 2015244 134 s/109 13:48:32 Ju +l 20 ---------------------------------------------------------------------- +---- FILENAME RECORD_ID M WAITING UNBR UNO TTY TIME DA +TE /prod-data/J 001!L!311895 X jmorg 5713932 191 ts/46 14:01:42 Ju +l 20 FILENAME RECORD_ID M OWNER UNBR UNO TTY TIME DA +TE /prod-datahi 001!10274882 X rfuse 3354796 61 ts/43 13:39:02 Ju +l 20 ---------------------------------------------------------------------- +---- FILENAME RECORD_ID M WAITING UNBR UNO TTY TIME DA +TE /prod-datahi 001!10274882 X jmorg 3584038 247 ts/49 13:39:22 Ju +l 20 :

This program print out the following (using Data::Dumper)

C:\Old_Data\perlp>perl t5.pl $VAR1 = { '001!L!311895' => { 'owner' => [ '/prod-data/J', '001!L!311895', 'X', 'jmorg', '2015244', '134', 's/109', '13:48:32', 'Jul 20' ], 'waiter' => [ [ '/prod-data/J', '001!L!311895', 'X', 'jmorg', '5713932', '191', 'ts/46', '14:01:42', 'Jul 20' ] ] }, '001!10274882' => { 'owner' => [ '/prod-datahi', '001!10274882', 'X', 'rfuse', '3354796', '61', 'ts/43', '13:39:02', 'Jul 20' ], 'waiter' => [ [ '/prod-datahi', '001!10274882', 'X', 'jmorg', '3584038', '247', 'ts/49', '13:39:22', 'Jul 20' ] ] }, '00151120273' => { 'owner' => [ '/prod-data/J', '00151120273', 'X', 'jmorg', '3584038', '247', 'ts/49', '13:38:12', 'Jul 20' ], 'waiter' => [ [ '/prod-data/J', '00151120273', 'X', 'jmorg', '2015244', '134', 's/109', '13:48:32', 'Jul 20' ], [ '/prod-data/J', '00151120273', 'X', 'gdavi', '1359996', '62', 'ts/20', '13:54:22', 'Jul 20' ] ] } }; C:\Old_Data\perlp>


Comment on Re: How to reference to array keys?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-11-21 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (104 votes), past polls