Re: groups of values from an array



Horvath, Peter wrote:
Hello Perl guru's,
I need to pull multiple values form an array. There are
separator array entries. I need to be able to get all the values between
the separator values, process them, and then move onto the next group
till the end. Here is an example of the array.

========================================================================
========
media ID: 001070
media type: 1/2" cartridge tape (6)
barcode: 001070
media description: V0|256|S190|20040315
volume pool: NBdatabase (5)
robot type: NONE - Not Robotic (0)
volume group: Offsite-V0
vault name: V0
vault sent date: Thu Jan 17 21:23:12 2008
vault return date: Thu Jan 31 19:51:59 2008
vault slot: 5038
vault session id: 8314
vault container id: -
created: Wed Feb 28 18:55:26 2001
assigned: Thu Jan 17 19:51:59 2008
last mounted: Thu Jan 17 20:58:00 2008
first mount: Wed Feb 28 19:01:24 2001
expiration date: ---
number of mounts: 148
max mounts allowed: ---
status: 0x1
========================================================================
========
[snip more data]


As Chas says, don't put the data into an array in the first place: you
have gained nothing by importing it into memory. The program below reads
a file structured like this into an array of hashes, which is likely to
be the most convenient form for any processing you need to do.

HTH,

Rob


use strict;
use warnings;

my @data;
my %record;

while (<DATA>) {

chomp;

if (/^==/) {
push @data, {%record} if %record;
%record = ();
}
elsif (/(.+):\s+(.+)/) {
$record{$1} = $2;
}
}
.



Relevant Pages

  • Re: groups of values from an array
    ... I need to pull multiple values form an array. ... vault return date: Thu Jan 31 19:51:59 2008 ... next unless defined $rec; #skip emtpy records ...
    (perl.beginners)
  • RE: groups of values from an array
    ... It is a command that I put to an array. ... with the while loop. ... vault return date: Thu Jan 31 19:51:59 2008 ... next unless defined $rec; #skip emtpy records ...
    (perl.beginners)
  • Re: FAangband bugs
    ... >> complete memory of detected grids. ... > centre of trap detection, and clear it when you get out of the area ... > of a vault, whether they glow, etc.). ... > either it needs to be enlarged, or a new array needs to be created. ...
    (rec.games.roguelike.angband)
  • groups of values from an array
    ... I need to pull multiple values form an array. ... separator array entries. ... vault return date: Thu Jan 31 19:51:59 2008 ... number of mounts: 148 ...
    (perl.beginners)