473,326 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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.
Jul 29 '08 #1
5 2313
KevinADC
4,059 Expert 2GB
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
3,509 Expert Mod 2GB
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
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
3,509 Expert Mod 2GB
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
4,059 Expert 2GB
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

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

Similar topics

1
by: nospam | last post by:
Hi, I have an application whose textareas rows automatically resize onFocus to the number of lines in the textArea. The cols are set to 100% (in a CSS file) in order to always take the full...
27
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this...
18
by: Vasilis Serghi | last post by:
Presently I define the number of lines to be expected in a file when defining the array size and the initialisation of this array. This works fine for now, but i'm sure that in the future this...
10
by: Craig Bumpstead | last post by:
Hi, I was wondering the best and fastest way to determine how many lines are in a log file. At the moment I am simply doing a StreamReader.ReadLine and incrementing a counter until I reach...
17
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
21
by: Imran | last post by:
I have a vector of integers, such as and I want to find out the number which occurs most frequently.what is the quick method. My array size is huge. what I am doing is 1. find out the...
2
by: ArtOfSpeech | last post by:
hi.... Can anyone tell me plz how to set a maximum number of (lines) to a rich text control and show only last linse when number of lines exceeds the maximum number??? i've tried to use...
6
by: shana07 | last post by:
Phew, I have problem..How to sort number in my files..I have these in my input files...: I need to sort the line in array from 12, 64, 8, 128 etc. 3 12 4 64 7 8 10 128 ... I just wanna...
4
by: CK938 | last post by:
Hi, I'm new to C and I'm trying to read in a file that can have an arbitrary number of lines, and put each line into a string array. I guess my problem is that I dont't want to have to explicitly...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.