Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

working with array and scalares in while loop

Status
Not open for further replies.

biobrain

MIS
Jun 21, 2007
90
0
0
GB
my code is as follow

Code:
        open (SP, "myfile");
        my $chains;
        my @chains;
	while(<SP>){
	     if ($_=~/^COMPND\s+\d+\sCHAIN:\s(.)/){
             $chains=$1;
             print "$chains";
             
             }
  	  }

the result print out

A K Y T C D E F values for $chains now i want top store all these in an array @chains i do not know how to do

i tried using split but it only give the one last enty F

also if i print $chains out side while loop it give only F

how i can take all the values for $chains out of while loop.

regards
 
Bio

You might need to post a sample of data...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
possibly:

Code:
        my @array;
        open (SP, "myfile");
        my $chains;
        my @chains;
    while(<SP>){
         if ($_=~/^COMPND\s+\d+\sCHAIN:\s(.)/){
             push @array,$1;
         }
    }
    print "$_\n" for @array;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Is that correct?
Code:
push @array, [red]$1[/red];
or would you want:
Code:
push @array, [red]$&[/red];
since $1 would only give the substring contained in the first set of parenthesis. or maybe just
Code:
if ($_=~/[red]([/red]^COMPND\s+\d+\sCHAIN:\s(.)[red])[/red]/) {
to get around slowing the regex.
 
While this does work:
Code:
my $chains;
my @chains;
For the sake of your sanity, I would suggest giving those two variables different names.
 
Is that correct?

I just went by what he posted, and he wanted $1.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top