By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

Find out the number of lines in an array

P: 14
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.
Jul 29 '08 #1
Share this Question
Share on Google+
5 Replies


KevinADC
Expert 2.5K+
P: 4,059
When you assign the value of an array to a scalar, the scalar will be the length of the array:

Expand|Select|Wrap|Line Numbers
  1. @array = qw(fee fie foe fum); 
  2. $n = @array;
  3. push @array2, $n;
  4.  
Jul 29 '08 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Jul 29 '08 #3

P: 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.

Expand|Select|Wrap|Line Numbers
  1. open (IN, $SNP) || die "nope\n";
  2.  
  3. while (<IN>) {
  4.  
  5.     chomp;
  6.  
  7.     $position = substr $_, 1, 10;               # extract the position
  8.     $TOP = substr $_, 13, 1;                    # extract the polymorphism type, same as ref if no polymorphism
  9.     $ref_seq = substr $_, 17, 1;                # extract the cerevisiae ref sequence
  10.     $para_seq = substr $_, 18, 1;               # extract the paradoxus sequence
  11.     $baya_seq = substr $_, 19, 1;               # extract the bayanus sequence
  12.  
  13.  
  14.     $baya_seq =~ s/\s+/x/g;                     # replace all blanks with 'x'
  15.  
  16. open (INFO, ">>$upstream") || die "nope\n";
  17.     if ($position gt $orf_beg) {
  18.         $A = "$position $ref_seq$para_seq$baya_seq\n";
  19.         print INFO "$A";
  20. }
  21.  
  22. print "$#A";
  23.  
  24. close (INFO);
  25.  
  26.  
  27.  
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.

Expand|Select|Wrap|Line Numbers
  1.  662376     [S] |GC | |   |
  2.  662375     [S] |CG | |   |
  3.  662374     [S] |GC | |   |
  4.  662373     [M] |CA | |   |
  5.  662371     [Y] |TC | |   |
  6.  662369     [M] |CAC| |   |
  7.  662367     [Y] |TCT| |   |
  8.  662365     [S] |GCG| |   |
  9.  662364     [R] |AGA| |   |
  10.  662360     [M] |CAC| |   |
  11.  662359     [W] |ATA| |   |
  12.  662358     [S] |CGC| |   |
  13.  662357     [M] |ACA| |   |
  14.  662356     [R] |GAG| |   |
  15.  662354     [S] |GCG| |   |
  16.  662353     [R] |AGA| |   |
  17.  662352     [R] |GAG| |   |
  18.  662350     [K] |TGT| |   |
  19.  662349     [Y] |CTC| |   |
  20.  662348     [M] |ACA| |   |
  21.  662347     [R] |GAG| |   |
  22.  662346     [S] |GCC| |   |
  23.  662345     [M] |CAC| |   |
  24.  662344     [S] |CGC| |   |
  25.  662343     [K] |GTG| |   |
  26.  662342     [S] |CGC| |   |
  27.  662340     [K] |TGT| |   |
  28.  662338     [W] |ATA| |   |
  29.  662336     [R] |GAG| |   |
  30.  662334     [S] |CG | |   |
  31.  662332     [S] |GC | |   |
  32.  662331     [K] |TG | |   |
  33.  662330     [Y] |TC | |   |
  34.  662328     [Y] |CT | |   |
  35.  662327     [S] |GC | |   |
  36.  662326     [M] |CA | |   |
  37.  662324     [S] |CG | |   |
  38.  662323     [K] |TG | |   |
  39.  662320     [R] |AG | |   |
  40.  662319     [S] |GC | |   |
  41.  662318     [W] |AT | |   |
  42.  662317     [R] |GA | |   |
  43.  662315     [S] |CG | |   |
  44.  662314     [M] |AC | |   |
  45.  662311     [W] |AT | |   |
  46.  662309     [S] |GC | |   |
  47.  662307     [S] |CG | |   |
  48.  662306     [W] |TA | |   |
  49.  662304     [Y] |CT | |   |
  50.  662303     [S] |GC | |   |
  51.  662300     [Y] |TC | |   |
  52.  662298     [S] |GC | |   |
  53.  662297     [K] |TG | |   |
  54.  
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.


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
Jul 29 '08 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Jul 30 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
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.
Jul 30 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.