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


Dear All,

my file looks like this

ICR000010::Proteinase inhibitor I25,
--ICR001713::Proteinase inhibitor I25A, stefin A::PR00295
ICR000014::PAS::PS50112::SM00091::TIGR00229
--ICR013655::PAS fold-3::PF08448
----ICR001321::Hypoxia-inducible factor-1 alpha, PAS

NOTE :
-- means one generation
---- means second generation

OUTPUT should be

Parent is ICR000010
ICR000010 First generation child is ICR001713
Parent is ICR000014
ICR000014 First generation child is ICR013655
ICR000014 Second generation child is ICR001321

here is my CODE to write the first line
##!/usr/bin/perl -w use strict; use warnings; open (FILE,"< C:/perl/ParentChildTreeFile.txt") or die "cannot open fi +le for reading: $!"; while (<FILE>){ if ($_ = /^(ICR\d+).*/){ my $parent = $1; print "\n Parent is $1"; } elsif ($_ = /^--(ICR\d+).*/){ my $firstchild = $1; print "\n $parent First generation child is $firstchild"; } elsif ($_ = /^----(ICR\d+).*/){ my $secondchild = $1; print "\n $parent Second generation child is $secondchild"; } }

OUTPUT :
Parent is ICR000010
Parent is ICR000014

Child Output is missing.I don't know why other if statements are not working. any help would be appreciated

Original code restored below by GrandFather

##!/usr/bin/perl -w use strict; use warnings; open (FILE,"< C:/perl/ParentChildTreeFile.txt") or die "cannot open fi +le for reading: $!"; while (<FILE>){ if ($_ = /^(IPR\d+).*/){ my $parent = $1; print "\n Parent is $1"; } elsif ($_ = /^--(IPR\d+).*/){ my $firstchild = $1; print "\n $parent First generation child is $firstchild"; } elsif ($_ = /^----(IPR\d+).*/){ my $secondchild = $1; print "\n $parent Second generation child is $secondchild"; } }

Replies are listed 'Best First'.
Re: I could not use more than one IF ($_ = / /) condition for a single line
by davorg (Chancellor) on Jul 01, 2009 at 16:26 UTC
    $_ = /^(IPR\d+).*/

    That doesn't do what you think it does.

    To match against a variable, you use the binding operator (=~) not the assignment operator.

    But, of course, you don't need to explicitly bind to $_ as that's the default operand for the match operator. So you can rewrite that as just:

    /^(IPR\d+).*/
    --

    See the Copyright notice on my home node.

    Perl training courses

      Now it's working... I used binding operator =~ instead of =. Thanks for your help guys
        you might want to look at the given/when block introduced in Perl 5.10 , it is very well suited for parsing , I like it more than the if blocks :)
      Sorry, I changed to ICR, it is not working still..
Re: I could not use more than one IF ($_ = / /) condition for a single line
by Anonymous Monk on Jul 01, 2009 at 16:25 UTC
    IPR is not the same as ICR , so your code/data doesn't match