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.
5 2313
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;
-
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
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.
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
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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).
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
| |