 | 
July 29th, 2008, 06:30 PM
| | Newbie | | Join Date: Jul 2008
Posts: 14
| | Find out the number of lines in an array
I was wondering if there was a way to find out how many lines were stored in an array, and then to be able to have that number stored in an array itself. I've been googling and searching the forum but cant find anything that works for me.
| 
July 29th, 2008, 07:01 PM
|  | Expert | | Join Date: Jan 2007
Posts: 3,662
| |
When you assign the value of an array to a scalar, the scalar will be the length of the array: - @array = qw(fee fie foe fum);
-
$n = @array;
-
push @array2, $n;
-
| 
July 29th, 2008, 07:04 PM
|  | Forum Leader | | Join Date: May 2007 Location: New Hampshire
Posts: 2,156
| |
By number of lines stored in an array, I assume that you mean you have taken a file and put it into an array, causing each line to be stored in its own index. Thus, you would need to know how many indexes there are.
So, you can use the special variable $#arrayname to get the highest index number(count starting at zero) and then just add 1 to it to get the number of indexes that exist (starting counting from 1).
Why not try coding what you want and then post the code here and we will help you.
Regards,
Jeff
| 
July 29th, 2008, 10:03 PM
| | Newbie | | Join Date: Jul 2008
Posts: 14
| |
Actually I am making the array in my code and need two outputs, one is where I am outputting the actual array to a file, and I also needed the number count to be output to yet another file. I have been writing a large code to do a few different things, but the section of the code that applies to this question is below. When I try the $#ArrayName it outputs the a lot of 1s that add up to the total number of characters, not the number of lines. -
open (IN, $SNP) || die "nope\n";
-
-
while (<IN>) {
-
-
chomp;
-
-
$position = substr $_, 1, 10; # extract the position
-
$TOP = substr $_, 13, 1; # extract the polymorphism type, same as ref if no polymorphism
-
$ref_seq = substr $_, 17, 1; # extract the cerevisiae ref sequence
-
$para_seq = substr $_, 18, 1; # extract the paradoxus sequence
-
$baya_seq = substr $_, 19, 1; # extract the bayanus sequence
-
-
-
$baya_seq =~ s/\s+/x/g; # replace all blanks with 'x'
-
-
open (INFO, ">>$upstream") || die "nope\n";
-
if ($position gt $orf_beg) {
-
$A = "$position $ref_seq$para_seq$baya_seq\n";
-
print INFO "$A";
-
}
-
-
print "$#A";
-
-
close (INFO);
-
-
-
The variables here are defined by STDIN earlier on in the script, for the data example below $orf_beg= 661376 and $orf_end= 654675. The file format that the data is in is as follows. -
662376 [S] |GC | | |
-
662375 [S] |CG | | |
-
662374 [S] |GC | | |
-
662373 [M] |CA | | |
-
662371 [Y] |TC | | |
-
662369 [M] |CAC| | |
-
662367 [Y] |TCT| | |
-
662365 [S] |GCG| | |
-
662364 [R] |AGA| | |
-
662360 [M] |CAC| | |
-
662359 [W] |ATA| | |
-
662358 [S] |CGC| | |
-
662357 [M] |ACA| | |
-
662356 [R] |GAG| | |
-
662354 [S] |GCG| | |
-
662353 [R] |AGA| | |
-
662352 [R] |GAG| | |
-
662350 [K] |TGT| | |
-
662349 [Y] |CTC| | |
-
662348 [M] |ACA| | |
-
662347 [R] |GAG| | |
-
662346 [S] |GCC| | |
-
662345 [M] |CAC| | |
-
662344 [S] |CGC| | |
-
662343 [K] |GTG| | |
-
662342 [S] |CGC| | |
-
662340 [K] |TGT| | |
-
662338 [W] |ATA| | |
-
662336 [R] |GAG| | |
-
662334 [S] |CG | | |
-
662332 [S] |GC | | |
-
662331 [K] |TG | | |
-
662330 [Y] |TC | | |
-
662328 [Y] |CT | | |
-
662327 [S] |GC | | |
-
662326 [M] |CA | | |
-
662324 [S] |CG | | |
-
662323 [K] |TG | | |
-
662320 [R] |AG | | |
-
662319 [S] |GC | | |
-
662318 [W] |AT | | |
-
662317 [R] |GA | | |
-
662315 [S] |CG | | |
-
662314 [M] |AC | | |
-
662311 [W] |AT | | |
-
662309 [S] |GC | | |
-
662307 [S] |CG | | |
-
662306 [W] |TA | | |
-
662304 [Y] |CT | | |
-
662303 [S] |GC | | |
-
662300 [Y] |TC | | |
-
662298 [S] |GC | | |
-
662297 [K] |TG | | |
-
The file goes on for thousands of lines. Basically the overall goal would be to cut the larger file into segments (which I have done with a series of if statements, the first of which is the one you see in the code), and to get the sequence and number of lines out into 2 different files. Quote: |
Originally Posted by numberwhun By number of lines stored in an array, I assume that you mean you have taken a file and put it into an array, causing each line to be stored in its own index. Thus, you would need to know how many indexes there are.
So, you can use the special variable $#arrayname to get the highest index number(count starting at zero) and then just add 1 to it to get the number of indexes that exist (starting counting from 1).
Why not try coding what you want and then post the code here and we will help you.
Regards,
Jeff | | 
July 30th, 2008, 01:05 AM
|  | Forum Leader | | Join Date: May 2007 Location: New Hampshire
Posts: 2,156
| |
You cannot use this in the manner you have. $A would have to be defined as @A, which is an array. $A is a string, not an array. $#arrayname ONLY works on arrays.
Regards,
Jeff
| 
July 30th, 2008, 03:52 AM
|  | Expert | | Join Date: Jan 2007
Posts: 3,662
| | Quote: |
Actually I am making the array in my code
| Not in the code you posted, there is no array anywhere in that code. At least there is nothing perl considers to be an array.
|  |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|