The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Array inside an array manas_ranjan UNIX for Advanced & Expert Users 5 06-10-2008 02:25 PM
I need help with an array! djsal Shell Programming and Scripting 1 11-28-2007 11:26 AM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 08:42 AM
array in awk asal_email2 Shell Programming and Scripting 5 06-13-2005 07:25 AM
Do I need an array here? TheCrunge Shell Programming and Scripting 5 04-19-2005 03:20 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-25-2008
ccp ccp is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 17
array

If I am given an array which contains 10 positive intergers, how to write in Unix shell returning any two numbers which sum up as 10, how about in perl which is easier
Thanks
  #2 (permalink)  
Old 02-25-2008
matt.d's Avatar
matt.d matt.d is offline
Registered User
  
 

Join Date: Feb 2008
Location: /dev/null
Posts: 62
Probably an easier way, but this will work:
Code:
#!/usr/bin/perl

@nums = qw{1 1 3 2 4 5 6 3 3 1};

# each integer has only one other integer that can be added to it to get 10

foreach (@nums) {
   $diff = 10 - $_;
   foreach $i(@nums) {
      if ($i == $diff) {
         print "$_ and $i add up to 10\n";
      }         
   }    
}
Just change the numbers inside the list to play around with it.
  #3 (permalink)  
Old 02-25-2008
ccp ccp is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 17
But I would assume I don't know any intergers in the array
so I need to make sure no intergers is last than 10, if so, the game is over,
if not, we can use double loop? how about if we don't know how many numbers in array ( not necessarily 10)
  #4 (permalink)  
Old 02-26-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
Quote:
Originally Posted by ccp View Post
But I would assume I don't know any intergers in the array
so I need to make sure no intergers is last than 10, if so, the game is over,
if not, we can use double loop? how about if we don't know how many numbers in array ( not necessarily 10)
You are not explaining your situation very well. Have you written any code yet to try and figure this out? How is the array going to be populated with numbers? Can there be duplicates in the array so you can have combinations like 5 + 5 = 10? Can there be many duplicates in the array so you can have something like 5 + 5 = 10 many times? Try and clarify your question or expect answers that are very general in nature.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0