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
Grabbing variables and comparing kerpm Shell Programming and Scripting 9 08-17-2008 06:45 PM
comparing 2 string variables in unix katdaniel16 UNIX for Dummies Questions & Answers 17 07-23-2008 03:25 AM
comparing content of 2 variables in script sam4now Shell Programming and Scripting 5 04-24-2008 03:42 PM
Comparing two variables rajip23 Shell Programming and Scripting 3 04-11-2008 06:45 AM
comparing variables MizzGail UNIX for Dummies Questions & Answers 3 02-08-2008 01:53 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-18-2008
kamitsin's Avatar
kamitsin kamitsin is offline
Registered User
  
 

Join Date: Nov 2006
Location: /dev/null
Posts: 177
Comparing Variables in Perl

Hi.

I have three arrays.

@a=('AB','CD','EF');
@b=('AB,'DG',HK');
@c=('DD','TT','MM');

I want to compare the elements of the first two array and if they match then so some substition.

I tried using the if statement using the scalar value of the array but its not giving me any output.

Help please !!!!!!!!!!!
  #2 (permalink)  
Old 09-18-2008
avronius avronius is offline VIP Member  
VIP Member
  
 

Join Date: Apr 2008
Location: Calgary
Posts: 305
Can you explain the comparison that you are looking for?
Is it one to one:
Code:
if ($a[0] = $b[0])
{
   your code here
}
Is it one to many:
Code:
for (@a){
   if ($b[0] = $_)
   {
      your code here
   }
}
Or is it many to many, in which case a database is much more efficient....
  #3 (permalink)  
Old 09-18-2008
kamitsin's Avatar
kamitsin kamitsin is offline
Registered User
  
 

Join Date: Nov 2006
Location: /dev/null
Posts: 177
if ($a[0] = $b[0])
{
your code here
}

As of now one to one but is not working.

Cheers.
  #4 (permalink)  
Old 09-18-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
Quote:
Originally Posted by avronius View Post
Can you explain the comparison that you are looking for?
Is it one to one:
Code:
if ($a[0] = $b[0])
{
   your code here
}
Is it one to many:
Code:
for (@a){
   if ($b[0] = $_)
   {
      your code here
   }
}
Or is it many to many, in which case a database is much more efficient....
None of your example compare anything, "=" is the assignment operator.

To compare numbers use "==" to compare strings use "eq", of course sometimes its never that simple but it looks like it would be in this case if the examples are what is really being compared. There are also modules that compare arrays, one I believe is called Array::Compare
  #5 (permalink)  
Old 09-18-2008
avronius avronius is offline VIP Member  
VIP Member
  
 

Join Date: Apr 2008
Location: Calgary
Posts: 305
If you wanted to loop through each element in an array to do a one-to-one evaluation for each element pair in an array, you can:

Code:
$elementCount = @a;
$count = 0;

while ( $count < $elementCount )
{
   if ( $a[$count] == $b[$count] )
   {
      print "\$a[$count]: $a[$count] = \$b[$count]: $b[$count]\n";
   }
   $count++;
}
(remember to use eq instead of == if you are working with text)

EDIT: changed line in red
  #6 (permalink)  
Old 09-18-2008
avronius avronius is offline VIP Member  
VIP Member
  
 

Join Date: Apr 2008
Location: Calgary
Posts: 305
For what it's worth, I had to edit your arrays in the example above:
Code:
@a=('AB','CD','EF');
@b=('AB','DG','HK');
@c=('DD','TT','MM');
  #7 (permalink)  
Old 09-18-2008
avronius avronius is offline VIP Member  
VIP Member
  
 

Join Date: Apr 2008
Location: Calgary
Posts: 305
Thanks Kevin - yes, I didn't go back to edit my first post (it started out as a basic framework) that morphed into a "code sample".
If you'll notice my second post in this thread does provide an actual comparison .

Cheers!
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 02:34 AM.


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