how to put "if" within a loop

Discussion in 'Perl Misc' started by Jie, Jul 17, 2007.

  1. Jie

    Jie Guest

    as you see below, I am trying to compare if a list of files have
    common elements. The following code works. The problem is that for the
    last two lines I need to manually write each array element. when the
    array is long, it is much awkward.

    is there a way to put the "if" inside a loop?

    thank you very much!

    Jie

    ==========my code============
    @lists = ("A", "B", "C");
    foreach $list (@lists) {
    %{$list} = ();
    open IN, " < $list.txt";
    while (<IN>) {
    $_ =~ /(rs\d+)\t([^\t]+)/;
    ${$list}{$1} = $2;
    }
    close IN;
    }

    foreach $SNP (%{$lists[0]}) {
    if ( exists ${$lists[1]}{$SNP} && exists ${$lists[2]}{$SNP}) {
    print OUT "$SNP\t${$lists[0]}{$SNP}\t${$lists[1]}{$SNP}\t${$lists[2]}
    {$SNP}\n";
    }
    }
     
    Jie, Jul 17, 2007
    #1
    1. Advertising

  2. Jie

    Mirco Wahab Guest

    Jie wrote:
    > is there a way to put the "if" inside a loop?
    > ==========my code============
    > @lists = ("A", "B", "C");
    > foreach $list (@lists) {
    > %{$list} = ();
    > open IN, " < $list.txt";
    > while (<IN>) {
    > $_ =~ /(rs\d+)\t([^\t]+)/;
    > ${$list}{$1} = $2;
    > }
    > close IN;
    > }
    >
    > foreach $SNP (%{$lists[0]}) {
    > if ( exists ${$lists[1]}{$SNP} && exists ${$lists[2]}{$SNP}) {
    > print OUT "$SNP\t${$lists[0]}{$SNP}\t${$lists[1]}{$SNP}\t${$lists[2]}
    > {$SNP}\n";
    > }
    > }


    Try (untested):

    ...
    foreach my $SNP (%{$lists[0]}) {
    print
    $SNP,
    join "\t",
    map ${$lists[$_]}{$SNP},
    grep exists ${$lists[$_]}{$SNP},
    0 .. $#lists;
    print "\n"
    }
    ...



    BTW: you should drop this code completely
    and rewrite a small idiomatic solution
    (only a few lines).

    The symbolic refs render this code unmaintainable
    and dangerous.

    Regards

    M.
     
    Mirco Wahab, Jul 17, 2007
    #2
    1. Advertising

  3. On Tue, 17 Jul 2007 19:01:19 -0000, Jie <> wrote:

    >@lists = ("A", "B", "C");


    use strict;
    use warnings;

    >foreach $list (@lists) {


    my $list

    > %{$list} = ();
    > open IN, " < $list.txt";


    # possibly better:
    open my $in, '<', "$list.txt"
    or die "something that includes $list and $!\n"

    > while (<IN>) {
    > $_ =~ /(rs\d+)\t([^\t]+)/;


    # either
    $var =~ /$rx/;
    # or
    /$rx/;

    >foreach $SNP (%{$lists[0]}) {


    Are you sure you don't want keys() there?

    > if ( exists ${$lists[1]}{$SNP} && exists ${$lists[2]}{$SNP}) {
    > print OUT "$SNP\t${$lists[0]}{$SNP}\t${$lists[1]}{$SNP}\t${$lists[2]}
    >{$SNP}\n";
    > }
    >}


    You can make your code slightly more agile:

    # untested
    my @lists = qw/A B C/;
    for (@lists) {
    open my $in, '<', "$_.txt"
    or die "Can't open `$_.txt': $!\n";
    /(rs\d+)\t([^\t]+)/ and ${$list}{$1}=$2 while <$in>;
    }

    for my $snp (keys %{$lists[0]}) {
    print $out (join "\t", $snp, map ${$lists[$_]}{$snp}, 0..2), "\n"
    if 2 == grep exists ${$lists[$_]}{$snp}, 1,2;
    }


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Jul 17, 2007
    #3
  4. On Jul 17, 8:01 pm, Jie <> wrote:
    > as you see below, I am trying to compare if a list of files have
    > common elements. The following code works. The problem is that for the
    > last two lines I need to manually write each array element. when the
    > array is long, it is much awkward.
    >
    > is there a way to put the "if" inside a loop?
    >
    > thank you very much!
    >
    > Jie
    >
    > ==========my code============
    > @lists = ("A", "B", "C");


    Why are you not declaring your variables?

    > foreach $list (@lists) {
    > %{$list} = ();


    Why are you using symbolic references?

    > open IN, " < $list.txt";


    Why are you using global filehandles?

    Why are you using the 2-arg open?

    All the above implies to me that you are learning to program in Perl4.

    Perl5 has been around a long time now. There's really no reason to
    learn Perl4. If you have a book or other resource that is teaching you
    Perl4 programming techniques I suggest you burn it.
     
    Brian McCauley, Jul 20, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. takayuki
    Replies:
    2
    Views:
    280
    Calvin Spealman
    Jun 16, 2008
  2. takayuki
    Replies:
    17
    Views:
    472
    John Salerno
    Jun 17, 2008
  3. addi
    Replies:
    0
    Views:
    271
  4. Jie

    how to put "if" within a loop

    Jie, Jul 17, 2007, in forum: Perl Misc
    Replies:
    0
    Views:
    68
  5. Isaac Won
    Replies:
    9
    Views:
    382
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page