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

I have an array of records, and I need to print each record with the same third key one one row. If the data in a row does not match that of the previous column key it should go to the next line and so on.

I have been helped with the following code, it it only works for the first column key and need it to work with with third. Furthermore, I will not work with multiple records for example:

0<tab>761<tab>94 (041352) 0<tab>2311<tab> 94 (0.00) 0<tab>27<tab> 94 (NG012) 0<tab>892<tab>94 (04/23/2002) 0<tab>494<tab> 94 (520-004) 1<tab>343<tab> 38 (info) 1<tab>133<tab> 38 (info) 1<tab>345<tab> 94 (info)
Desired Output:
0<tab>761<tab>94 (041352) 0<tab>2311<tab> 94 .... 1<tab>343<tab> 38 (info) 1<tab>133<tab> 38 (info) 1<tab>345<tab> 94 (info)
Sorting is not need as in the code I am about to show you for the list has been sorted. I only need to compare by columns. In the first record, put all recs with 94s on one row:
.....94....94....94... .....38....38 .....94
Please help...
thanks

Edited: formatting dvergin 2002-05-10

Replies are listed 'Best First'.
Re: Print record by column key
by Mr. Muskrat (Canon) on May 10, 2002 at 16:53 UTC
      Hey thanks for the help!!! ^_^ Would know a way that I can remove the numbers and tabs between parenthesis of data stored in a file. I have tried using the grep command but it appeard to be greedy. It removes more info than needed. The following is an example of the data in the file: (The)<space>0<tab>257<tab>38(Name)<space>0<tab>501<tab>38() (Date)<space>1<tab>144<tab>94(REAL)<space>1<tab>65<tab>94() ( )<space>0<tab>2219<tab>2338(1120) (You Are Here) ( ) (Date)<space>0<tab>1447<tab>2571(today) How can I remove all data (numbers, tabs, and spaces) between each left and right parenthesis )remove( except for one <tab>,/t. Needed output: (The)<tab>(Name)<tab>() (Date)<tab>(REAL)<tab>() ( )<tab>(1120) (You Are Here) ( ) (Date)<tab>(today) Your help will be greatly appreciated.
        Since I can't really tell which characters are tabs and which are spaces, I'll give you a solution that will remove word characters.
        I'll let you figure out how to remove the tabs and spaces.
        #!/usr/bin/perl -w use strict; $line = "(The)025738(Name)050138()\t(Date)114494(REAL)16594()\t( )0221 +92338(1120)\t(You Are Here)\t( )\t(Date)014472571(today)\n"; $line =~ s/\)\w+\(/)(/g; # match on one or more word characters sandwi +ched between ) and ( print $line;

        Who says that programmers can't work in the Marketing Department?
        Or is that who says that Marketing people can't program?
Re: Print record by column key
by vladb (Vicar) on May 10, 2002 at 15:05 UTC
    This should work (see code comments):
    use strict; # will use this variable to hold third # column value of the last record. my $last_val; while(<DATA>) { chomp; next unless (m/.*<tab>.*<tab>\s*(\d{2,})/); # third column value has changed? print "\n" unless ($last_val == $1); # yes, skip to next line print; $last_val = $1; } __DATA__ 0<tab>761<tab>94 (041352) 0<tab>2311<tab> 94 (0.00) 0<tab>27<tab> 94 (NG012) 0<tab>892<tab>94 (04/23/2002) 0<tab>494<tab> 94 (520-004) 1<tab>343<tab> 38 (info) 1<tab>133<tab> 38 (info) 1<tab>345<tab> 94 (info)
    Here's the output I get:
    0<tab>761<tab>94 (041352) 0<tab>2311<tab> 94 (0.00) 0<tab>27<tab> 94 ( +NG012) 0<tab>892<tab>94 (04/23/2002) 0<tab>494<tab> 94 (520-004) 1<tab>343<tab> 38 (info)1<tab>133<tab> 38 (info) 1<tab>345<tab> 94 (info)


    "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith