Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old July 29th, 2008, 06:30 PM
Newbie
 
Join Date: Jul 2008
Posts: 14
Default 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.
Reply
  #2  
Old July 29th, 2008, 07:01 PM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Posts: 3,662
Default

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.  
Reply
  #3  
Old July 29th, 2008, 07:04 PM
numberwhun's Avatar
Forum Leader
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,156
Default

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
Reply
  #4  
Old July 29th, 2008, 10:03 PM
Newbie
 
Join Date: Jul 2008
Posts: 14
Default

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.


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
Reply
  #5  
Old July 30th, 2008, 01:05 AM
numberwhun's Avatar
Forum Leader
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,156
Default

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
Reply
  #6  
Old July 30th, 2008, 03:52 AM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Posts: 3,662
Default

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.
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles