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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Problem

Status
Not open for further replies.

lmbylsma

Programmer
Nov 11, 2003
33
US
I can't figure out what is wrong with my code. It's supposed to create an array and print it out, but nothing prints, I don't get any error messages or anything.

Here is my code:

while (<>) {
chomp;
push @isr, [split/\t/];
}


for my $i(0..$#isr){

if ($isr[$i][5] eq &quot;10&quot;){

push @trialtype1, $isr[$i][3];

}

}

print @trialtype1;


Basically it reads a data file that looks something like this:

1 287 3 123456 ABBABA 90
1 287 3 123456 ABBABA 90
1 30 9 123456 ABBBAA 100
1 55 5 125346 ABBAAB 80
1 55 5 125346 ABBAAB 80
1 285 2 651234 BABAAB 70
1 224 1 641253 ABABAB 60
1 204 5 612435 BAABBA 80
1 204 5 612435 BAABBA 80
1 204 5 612435 BAABBA 80
1 204 5 612435 BAABBA 80
1 204 5 612435 BAABBA 80
1 113 4 231456 AABBAB 30
1 113 4 231456 AABBAB 30
1 234 6 243156 BBABAA 20
1 234 6 243156 BBABAA 20
1 227 10 164253 BBBAAA 10
1 184 10 143256 AAABBB 10
1 184 10 143256 AAABBB 10
1 184 10 143256 AAABBB 10
1 84 3 124365 BAABAB 90
1 84 3 124365 BAABAB 90


So my codes takes each lines and puts each number or &quot;word&quot; into a separate element in the array. The elements I am currently interested are the [3] and [5] positions on each row.

What I want to do is look for the ones that contain a 10 in position [5] and put just the [3] element of that line into another array. Then print it out. However, it doesn't seem to be working since nothing is printed out.

I'm using eq instead of == because for some reason it thinks its not a number, but I don't see how that should make a difference.

 
You need to say $isr[$i]->[5] and $isr[$i]->[3] (note the arrows). Your split statement creates an
anonymous array, and the brackets around the split create
a reference to that anonymous array. You're then
pushing this array reference into the array @isr, so @isr
is an array whose elements are array references.
The -> is a dereference operator. If you don't understand
what I'm talking about, you should read up on references.
Type perldoc perlref at a command prompt.

However, when I cut and pasted what you posted, just making
the above change didn't cause anything to be printed out.
I changed the \t in your split statement to \s+, and then got printout. So maybe your data isn't
actually tab-delimited? (\s+ means &quot;one or more whitespace
characters&quot;, which includes tabs and spaces.)

Also, I was able to use &quot;== 10&quot; in the comparison in the
second loop. (eq &quot;10&quot; also worked.)

Here's your code with my changes. Note that I wrote a
third loop to make the elements of @trialtype1 print on separate lines.

Code:
while (<>) {
    chomp;
    push @isr, [split/\s+/];    
}

for my $i (0..$#isr){    
    if ($isr[$i]->[5] == 10){
        push @trialtype1, $isr[$i]->[3];
    }
}

print &quot;$_\n&quot; for (@trialtype1);

Please don't take this the wrong way, but I agree with
icrf's observation in an earlier thread that it sounds like
you need to sit down with a good book on Perl and study up
on how things work. Get yourself a copy of Learning Perl if you don't already have one. I commend your
persistence, but you'll progress a lot faster if you've
done some reading. Online or built-in documentation is
great, but I found the Llama Book (Learning Perl)and the Camel Book (Programming Perl) invaluable when I
was starting out with Perl, and still refer to the Camel.
The Llama book doesn't cover some more advanced topics
(I don't think it covers references, for example), but is
a good place to start.
 
I have Learning Perl (and I read the entire thing) it doesn't even cover multidim arrays. I just got the Programming Perl book yesterday and am working on going through it.

I read somewhere in the perldoc that @array[1]->[2] and @array[1][2] mean exactly the same thing, is this not the case? Or its only exactly the same thing under certain circumstances?

I'm sorry if you guys think I'm not trying, but just by reading something doesn't make me automatically understand it, programming comes more easily for some than others. I tried this particular problem several different ways and worked on it for hours, but I couldn't figure it out. I never would've thought to try [1]->[2] format since the documentation I read said they were exactly the same thing.

Thanks for your help.

 
I can see that you learned how to create a multidimensional array based off of my response to your post here:

thread219-712207 for the acknowledgement.


What I am really curious about though, is why are you making this MUCH more difficult than it has to be? Why the multidim arrays and then sorting through them just to push certain values back into a regular array? You can accomplish the exact same thing you are doing by using a simple split.

use strict;
open (IN, &quot;c:/temp/test.txt&quot;);
my @trialtype1;
while (<IN>) {
push @trialtype1, (split/\s+/)[3] if ((split/\s+/)[5] == 10);
}
print &quot;@trialtype1\n&quot;;
 
You got me on the arrows -- the documentation does say
they're always optional between brackets and apparently
that's true since it works without them. I was under
the impression this was only true under certain
circumstances, but I stand corrected. (I still like them
for clarity, though.)

I didn't mean to imply at all that you're not trying,
since clearly, you are. Sorry if it came off the wrong
way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top