FAQ
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);

Search Discussions

  • Raja Vadlamudi at Sep 4, 2008 at 5:02 pm
    On Thu, Sep 4, 2008 at 12:36 PM, Raja Vadlamudi wrote:
    On Thu, Sep 4, 2008 at 11:22 AM, Bobby 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: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    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.
  • John W. Krahn at Sep 4, 2008 at 7:29 pm

    Raja Vadlamudi wrote:
    On Thu, Sep 4, 2008 at 12:36 PM, Raja Vadlamudi wrote:
    On Thu, Sep 4, 2008 at 11:22 AM, Bobby wrote:

    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);
    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";
    With the parentheses around open's arguments it doesn't matter if you
    use '||' or 'or'. Without the parentheses only 'or' will work correctly.



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
  • Jenda Krynicky at Sep 5, 2008 at 12:03 am
    From: Bobby <cybercruiserz@yahoo.com>
    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");
    1. If you have several arrays (or hashes) that contain data in the
    same format and you need to do something with all of them, you should
    have them all in an array or hash. In this particular case a hash:

    %size = (
    A => ["A","B","C"],
    B => ["D","E","F"],
    C => ["G","H","I"],
    );

    Now if you need to do something with all of them you can use

    foreach my $array (values %size) {...}

    or

    while (my ($key, $array) = each %size) { ... }

    Even if you do not know yet whether you'll need to do something with
    all of them, as soon as you have several variables named $something1,
    $something2, $something3, ... or $somethingA, $somethingB,
    $somethingC, ... you should stick all that data in an array or hash
    of somethings.

    2. If you need to be able to find out easily and quickly whether
    something is in a set of values, use a hash, not an array. It's much
    quicker to ask whether

    exists $set{$key}
    in a
    %set = (key1 => 1, key2 => 1, key3 => 1,...)
    than whether
    grep {$_ eq $key} @set
    in
    @set = (key1, key2, key3, ...)

    If you already have the data in an array you can create the hash by

    my %set = map {$_ => 1}, @list;

    or

    my %set, @set{@list} = ();

    (In the first case you can use just
    if ($set{$key})
    in the second you have to use the exists().)

    In this case you do not actually want to go through all the groups
    and find out which one contains the key you are looking for. You want
    to find out what section maps the key to. So you'd better store the
    info like this:

    %map = (
    A => 'A',
    B => 'A',
    C => 'A',
    D => 'B',
    E => 'B',
    F => 'B',
    G => 'C',
    H => 'C',
    I => 'C',
    );

    and then if you want to know what group does a letter belong to you
    just ask
    my $group = $map{$letter};

    simple and as quick as it can get.


    Choose the right datastructure and the code will become obvious.

    Jenda
    ===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedSep 4, '08 at 3:22p
activeSep 5, '08 at 12:03a
posts4
users4
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase