Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-08-2008
Member
 
Join Date: May 2008
Posts: 2,676
Counting numbers and deleting array

I am trying to delete an element from an array if it exists and count the number of item afterwards

I wrote
Code:
anArray = [1,2,3,4,5,6,7,8,9]
anArray.size
=> 9
anArray.map {|e| e if e != 4}.compact
=> [1, 2, 3, 5, 6, 7, 8, 9]
anArray.map {|e| e if e != 4}.compact.size
=> 8
Is ther a better way to do it... it it's enough ? (learning always how to write better code..)

Thanks
Reply With Quote
  #2  
Old 25-08-2008
Member
 
Join Date: Apr 2008
Posts: 1,948
Quote:
Originally Posted by SalVatore View Post
I am trying to delete an element from an array if it exists and count the number of item afterwards
How about using Array#delete_if?
Reply With Quote
  #3  
Old 25-08-2008
Member
 
Join Date: Jan 2008
Posts: 1,515
Array#delete works also with no need for a block (i.e. you're only checking for equality)

anArray = [1,2,3,4,5,6]
p anArray.delete(4)
p anArray.size
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Counting numbers and deleting array"
Thread Thread Starter Forum Replies Last Post
How to change negative numbers to positive numbers in Excel Shaina Na Microsoft Project 3 08-01-2012 05:35 PM
Need C program code to count numbers of negative & positive numbers Sarfaraj Khan Software Development 5 16-01-2010 02:00 PM
Want an array of Random numbers InterNetWorKed Software Development 4 03-11-2009 02:17 PM
Loop counting odd and even numbers Allan.d Software Development 3 23-10-2009 11:40 AM
SQL Query for Searching Missing Numbers from Sequence of Numbers Bhagwandas Software Development 3 18-02-2009 01:47 PM


All times are GMT +5.5. The time now is 10:07 PM.