On Thu, Sep 4, 2008 at 12:36 PM, Raja Vadlamudi <[EMAIL PROTECTED]> wrote:

>
>
> On Thu, Sep 4, 2008 at 11:22 AM, Bobby <[EMAIL PROTECTED]> wrote:
>
>> Hi,
>>
>> I have a large pipe delimited text file that i want to loop through and
>> sort out a column of data within that file. Let's call this column $sizes. I
>> want to group these different sizes into categories. i.e.
>> @sizeA = ("A","B","C");
>> @sizeB = ("D","E","F");
>> @sizeC = ("G","H","I");
>>
>> This is what i want to do:
>>
>> If $sizes is in @sizeA
>> print the sizes that are listed in @sizeA
>>
>> If $sizes is in @sizeB
>> print the sizes that are listed in @sizeB
>>
>> same for @sizeC.
>>
>> Could someone show me how to do this? Below is what i have so far. Thanks.
>>
>> #!/usr/bin/perl
>> use strict;
>> use warnings;
>>
>> open(IN, $ARGV[0]) || die "Could not open data file: $ARGV[0]\n";
>> my @sizes = (<IN>);
>>
>> foreach my $line (@sizes)
>> {
>>        # split rows into columns
>>        my @fields = split "\t", $line;
>>        my $size = $fields[2];
>> }
>> close(IN);
>>
>>
>>
>>
>>
>>
>>
>> --
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>> http://learn.perl.org/
>>
>>
>>
 One mistake would be using '||' instead of 'or' in open. Use this instead.

open(IN, $ARGV[0]) or die "Could not open data file: $ARGV[0]\n";

Instead of arrays, you might want to use hashes to make things easier.

%sizeA = ("A","1","B","1","C","1");
%sizeB = ("D","1","E","1","F","1");
%sizeC = ("G","1","H","1","I","1");

In the for loop, you can say

my $size = $fields[2];
if ($sizeA{$size} == 1) { print for (keys %sizeA); }
elsif ($sizeB{$size} == 1) { print for (keys %sizeB); }
elsif ($sizeC{$size} == 1) { print for (keys %sizeC); }
else { print "I dont have $size"; }

It depends on how around data you have. If it is too too huge, then hashes
might not be preferable.

Reply via email to