Re: groups of values from an array



On Jan 29, 2008 9:25 AM, Horvath, Peter <phorvath@xxxxxxxxxxxx> 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.
snip

That sure doesn't look like an array, that looks like a file that has
been read into an array. Reading files into an array is a bad idea
(reading data multiple times, memory consumption, etc). Try a while
loop instead:

#!/usr/bin/perl

use strict;
use warnings;

{
local $/ = ("=" x 80) . "\n"; #set the record separtor
while (my $rec = <DATA>) {
chomp($rec); #remove record separator
next unless length($rec); #skip empty records
print "record:\n", map { "\t$_\n" } split /\n/, $rec;
}
}

__DATA__
================================================================================
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
================================================================================
media ID: 001071
media type: 1/2" cartridge tape (6)
barcode: 001071
media description: V0|432|S3840|00000000
volume pool: NBdatabase (5)
robot type: NONE - Not Robotic (0)
volume group: Offsite-V0
vault name: V0
vault sent date: Fri Jan 18 02:35:30 2008
vault return date: Fri Feb 01 00:01:26 2008
vault slot: 4884
vault session id: 8316
vault container id: -
created: Wed Feb 28 19:50:06 2001
assigned: Fri Jan 18 00:01:25 2008
last mounted: Fri Jan 18 01:23:39 2008
first mount: Wed Feb 28 20:01:38 2001
expiration date: ---
number of mounts: 141
max mounts allowed: ---
status: 0x1
================================================================================
media ID: 001072
media type: 1/2" cartridge tape (6)
barcode: 001072
media description: V0|368|S5180|00000000
volume pool: NBdatabase (5)
robot type: NONE - Not Robotic (0)
volume group: Offsite-V0
vault name: V0
vault sent date: Mon Jan 21 09:47:51 2008
vault return date: Mon Feb 04 07:52:31 2008
vault slot: 5828
vault session id: 8336
vault container id: -
created: Wed Feb 28 19:50:06 2001
assigned: Mon Jan 21 07:52:30 2008
last mounted: Mon Jan 21 08:56:36 2008
first mount: Wed Feb 28 20:02:24 2001
expiration date: ---
number of mounts: 146
max mounts allowed: ---
status: 0x1
================================================================================

If you really do have an array, your code should look like this
(warning untested)

my @lines = however_you_get_the_array_of_lines();
my @records;
my $rec;
for my $line (@lines) {
if ($line eq ("=" x 80) . "\n") {
next unless defined $rec; #skip emtpy records
push @records, $rec;
$rec = undef;
next;
}
$rec .= $line;
}
.



Relevant Pages

  • 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)
  • Re: 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 ...
    (perl.beginners)
  • 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)
  • Re: Optimizing extraction of elements from large dyn arrays
    ... >>> obviously the deeper you get into the array the slower the crawl. ... then do a LOOP WHILE READNEXT thing? ... >> SELECT REC ...
    (comp.databases.pick)