[oak perl] Perl Script

Kester Allen kester at gmail.com
Thu Jun 16 08:55:28 PDT 2005


Hi Ken--

Here's a quick cut at your problem.  The technique I used is to build
up a hash, using the numbers as the keys and an array of the numbers
as the values, so it'll end up looking something like this:

%hash = (
             '7240185' => [ 'A', 'B', 'C' ],
             '7240194' => [ 'B', 'C' ],
             '7240190' => [ 'A', 'B', 'C' ],
             '7240207' => [ 'A', 'B' ],
             '7240943' => [ 'C' ]
         );

I've used two techniques to determine if the number should go into
file2 or file3, one based on counting the number of elements in the [
'A', 'B', 'C' ] array, and one based on matching the text in it.

--Kester

------------start code------------
#!/usr/bin/perl

use warnings;
use strict;

# Assumes the first line of the data will have two columns.

my $key = "";
my %output;

# Make a hash of COLUMN1 => [ each COLUMN2 entry ]
while (<DATA>) {
    my @cols = split;

    $key = shift @cols
        if 2 == scalar @cols; # If there are two columns

    push @{$output{$key}}, $cols[0];
}

# Count the entries in the value arrays to see while file the output should
#   go into:
#
foreach ( keys %output ) {
    print "$_ goes in file number ",
          ( 3 == scalar @{$output{$_}} )
              ? "2\n"
              : "3\n";
}
print "\n\n";


# Another technique-- verify that the text in the value arrays is correct:
#
my $full_match_regex = '^ABC$';
foreach ( keys %output ) {
    my $col2_vals = join "", sort @{$output{$_}};
    print "$_ goes in file number ",
          ( $col2_vals =~ m/$full_match_regex/ )
              ? "2\n"
              : "3\n";
}

__DATA__
7240185 A
        B
        C
7240943 C
7240207 A
        B
7240194 B
        C
7240190 A
        B
        C
------------end code------------


On 6/16/05, kenneth Jideofor <jideoforkc at yahoo.com> wrote:
> Hi George,
> 
> Good reading from you.
> It's my pleasure having my name in your mailing list.
> 
> I believe this is a very good opportunity for me to
> develop my self on Perl.
> I look forward to getting what it takes to become a
> Perl guru from the group.
> 
> Meanwhile, I have this task to perform and I believe
> Perl will offer me a better solution to accomplishing
> it.
> 
> I have tried doing it with an awk script but I
> couldn't get exactly what I wanted.
> I need an assistance on how to go about it with Perl
> script.
> 
> I have a file (FILE1)made up of two columns of data.
> Column 1 contains numbers while Column 2 contains
> letters. FILE1 is as shown below:
> 
> FILE1:
> __________________
> 7240185 A
>         B
>         C
> 7240943 C
> 7240207 A
>         B
> 7240194 B
>         C
> 7240190 A
>         B
>         C
> ____________________
> 
> I need to produce two files (FILE2 and FILE3)from
> FILE1 based on the following conditions:
> 
> Each number in Column 1 is expected to have
> corresponding letters A, B, and C in Column 2. I need
> to print out to FILE2 all the numbers that do not have
> their corresponding letters A, B, and C, while those
> that have their corresponding letters A, B, and C
> should be saved in FILE3.
> 
> >From FILE1, the following numbers
> do not have their corresponding letters A, B, and C:
> 
> 7240943
> 7240207
> 7240194
> 
> while the following numbers have their corresponding
> letters A, B, and C
> 
> 7240185
> 7240190
> 
> Hence, FILE2 should contain the following numbers:
> 
> 7240943
> 7240207
> 7240194
> 
> while FILE3 should contain the following numbers:
> 
> 7240185
> 7240190
> 
> Could anyone, please, assist me with a Perl solution
> to perform this task?
> 
> Thanks.
> 
> Best regards,
> Ken
>


More information about the Oakland mailing list