Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can I add an identifier to differentiate any block of data coming from an array?
I know that the data will come in blocks, I would like to be able to differentiate each block by comparing the first tag <1> with any other first tag in this array, if there is a match I would like to add something like
<1><2><3>...<1b><2b><3b>...<1c><2c><3c><4>...<1d><2d><3d>....
How could I do something like that? Data will be in one or more blocks like these:
<1>66543 <2>2009 <3>TY <4>- <6> <7>TOM <8>2 - DUNDAS <8>100 - MAIN <9>ONT 00001 <10> <11>000-000-0000 <12> <1>12345 <2>1999 <3>PP <4>XLX - TEST <6>Comm <7> <8>1 BLVD <9>MA 00001 <10>CITI, INC. <11>000-000-0000 <12> <1>8887 <2>2000 <3> <4>TEST <6> <7>JOE <7>MARY <8>100 MAIN <9>CT 02221 <10>BH, INC. <11>871-000-0000 <12>YEAR <1>test <2>2087 <3>XX <4>- <6> <7>DOM <8>2 - CHT <8>1000 - CAR <9>ONTA X0001 <10> <11>000-000-0000 <12> <13> <14>CB

my @all; # here is all the data will be foreach my $xy (@all) { }


It would be nice to have at the end something like this:
<1>66543 <2>2009 <3>TY <4>- <6> <7>TOM <8>2 - DUNDAS <8>100 - MAIN <9>ONT 00001 <10> <11>000-000-0000 <12> <1b>12345 <2b>1999 <3b>PP <4b>XLX - TEST <6b>Comm <7b> <8b>1 BLVD <9b>MA 00001 <10b>CITI, INC. <11b>000-000-0000 <12b> <1c>8887 <2c>2000 <3c> <4c>TEST <6c> <7c>JOE <7c>MARY <8c>100 MAIN <9c>CT 02221 <10c>BH, INC. <11c>871-000-0000 <12c>YEAR <1d>test <2d>2087 <3d>XX <4d>- <6d> <7d>DOM <8d>2 - CHT <8d>1000 - CAR <9d>ONTA X0001 <10d> <11d>000-000-0000 <12d> <13d> <14d>CB

Thanks for the help!!

Replies are listed 'Best First'.
Re: Adding an identifier to an array element.
by apl (Monsignor) on Apr 15, 2010 at 19:35 UTC
    Tell us what @all looks like. Is it a one-dimensional array composed of the N lines of a block? Is it a 2-dimensional array where you store each block in thr array?

    What is <1> as opposed to <1c>? Something to be displayed, or manipulated, or an index into your array, or...?

    You seem to be suffering from I know what I mean. Why don't you?.

    REVISED: That last was uncalled for. I just reread your post. Why not simply maintain a "counter" of a single character (null initially, then 'a', 'b', etc. Be careful what happens after 'z'), incrementing the counter each time you hit a record starting <1>? Then you could output it at will...

      So you know what he means... Why don't I? : )

      (Even if he wants to add a..z to each block it's completely unknown how his input can be split...

      In short: What?)

Re: Adding an identifier to an array element.
by Anonymous Monk on Apr 15, 2010 at 18:44 UTC
    what?
Re: Adding an identifier to an array element.
by Marshall (Canon) on Apr 16, 2010 at 11:44 UTC
    Here is one way to get your stated desired output from your stated input...
    #!/usr/bin/perl -w use strict; my $letter ='a'; while (<DATA>) { print_record(); print "\n"; # record spacer # $letter++; } # adds an alpha character to the id that's like <1> # except weirdly enough for a. # sub print_record { s/<(\d+)>/<$1$letter>/ unless $letter eq 'a'; print; while (defined($_ = <DATA>) && $_ !~ /^\s*$/) { s/<(\d+)>/<$1$letter>/ unless $letter eq 'a'; print; } } =prints: <1>66543 <2>2009 <3>TY <4>- <6> <7>TOM <8>2 - DUNDAS <8>100 - MAIN <9>ONT 00001 <10> <11>000-000-0000 <12> <1b>12345 <2b>1999 <3b>PP <4b>XLX - TEST <6b>Comm <7b> <8b>1 BLVD <9b>MA 00001 <10b>CITI, INC. <11b>000-000-0000 <12b> <1c>8887 <2c>2000 <3c> <4c>TEST <6c> <7c>JOE <7c>MARY <8c>100 MAIN <9c>CT 02221 <10c>BH, INC. <11c>871-000-0000 <12c>YEAR <1d>test <2d>2087 <3d>XX <4d>- <6d> <7d>DOM <8d>2 - CHT <8d>1000 - CAR <9d>ONTA X0001 <10d> <11d>000-000-0000 <12d> <13d> <14d>CB =cut __DATA__ <1>66543 <2>2009 <3>TY <4>- <6> <7>TOM <8>2 - DUNDAS <8>100 - MAIN <9>ONT 00001 <10> <11>000-000-0000 <12> <1>12345 <2>1999 <3>PP <4>XLX - TEST <6>Comm <7> <8>1 BLVD <9>MA 00001 <10>CITI, INC. <11>000-000-0000 <12> <1>8887 <2>2000 <3> <4>TEST <6> <7>JOE <7>MARY <8>100 MAIN <9>CT 02221 <10>BH, INC. <11>871-000-0000 <12>YEAR <1>test <2>2087 <3>XX <4>- <6> <7>DOM <8>2 - CHT <8>1000 - CAR <9>ONTA X0001 <10> <11>000-000-0000 <12> <13> <14>CB
      Thank you very much!