Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditions with Arrays +

Status
Not open for further replies.

Zen20

Programmer
Jul 30, 2008
5
0
0
US
Hi Everybody,

I am new registered user however I've been reading this great forum for a long time. However did not find answer to the following question:
###

$a = "1A"; # values will be changing
$b = "52"; # values will be changing
$c = "7_12"; # values will be changing

@myArray = ($b,$a,$c);

sub myArray_logic
{
my ($count) = @_;

if ( $count == 1 ){
$X = "some_String";
$Y = "some_Other_Sting";
$Z = "string";
}
elsif ( $count == 2 or $count == 3 )
{
$X = "StRinG";
$Y = "other_sTriNg";
}

This works fine for me, but it is based on the count of the array and if I have another array of change the order count, I would need to change all my conditions.

One more time:

If $count == 0 - it is "52"
I need my script to ALSO know that it is $b

Thank you in advance,
Zen20.net
 
Your question is unclear to me.
Please do the following:
-post a working piece of code between [ignore]
Code:
[/ignore] tags :it will show up more clearly. Note that your sub is unterminated and returns what?
-include in your code the part that calls the sub, so that we can understand its purpose
-describe your goal

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Perhaps it would be better if you tell us what data you have, and what you are trying to achieve. Then we could help you to find a solution.

In the meantime though, don't use $a and $b as general variables - they are special variables used by sort routines in Perl.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thank you for your replies.

I have a script that automatically installs different software builds from different releases. After each installation it does some tasks and then installs next build.

Please let me know if you have any questions. I know it is possible, I just need to explain better : )


NOTE: **Conditions are different for each release**

## Let's say we have three Releases and we know their names and numbers
## $REL_1 - RED - 2.5
## $REL_2 - BLUE - 3.0
## $REL_3 - GREEN - 3.5


Code:
#!/usr/bin/perl

$|=1;
use Net::FTP;
use Net::Telnet;
use Cwd;

#Builds
$REL_1_New_Build = "15";
$REL_1_Old_Build = "12";

$REL_2_New_Build = "45";
$REL_2_Old_Build = "40";

$REL_3_New_Build = "56";
$REL_3_Old_Build = "52";

#Arrays

@myArray_1 = ($REL_1_New_Build,$REL_2_New_Build,$REL_3_Old_Build,);

@myArray_2 = ($REL_3_New_Build,$REL_2_Old_Build,$REL_3_Old_Build,);

@myArray_3 = ($REL_3_New_Build,$REL_2_Old_Build,$REL_3_Old_Build,);


## Then user gets the menu:
## Array will be chosen based on the input

print "Please choose what release to test"
print "\n\tFor RED enter \"1\"";
print "\n\tFor BLUE enter \"2\"";
print "\n\tFor GREEN enter \"3\"";


while (($r_num < 1) || ($r_num > 3)) {
print "\n\nWhat Release?:";
chomp ($r_num = <STDIN>);

###
## If user chooses 1 - script will be running based on myArray_1
###

if ($r_num == 1) {

	$REL_NAME = "RED";
	$REL_NUM = "2.5";
	$countArray = @myArray_1-1;
	foreach $count (0..$countArray){
			myArray_1_logic($count); ## Here I call "myArray_1_logic" SUB
					}
		}

elsif ($r_num == 2) {

	$REL_NAME = "BLUE";
	$REL_NUM = "3.0";
	$countArray = @myArray_2-1;
		foreach $count (0..$countArray){
			myArray_2_logic($count);
						}
	
			}

elsif ($r_num == 3) {

	$REL_NAME = "GREEN";
	$REL_NUM = "3.5";
	$countArray = @myArray_3-1;
	foreach $count (0..$countArray){
			myArray_3_logic($count);
			
						}
					}



# Arrays Logic
##
## The following is what I want to simplify and have just one logic set. (not for every array) 
## If we look at @myArray_1 - count 0 - it is "$REL_1_New_Build"
# and script knows that it is "15"
# BUT what it doesn't know that it is also REL_1.
# It doesn't see the name of "$REL_1_New_Build" 
# just value 15. May be I should use double array or some lists...
##

sub myArray_1_logic
{
my ($count) = @_;
$LINE_VAR = $myArray_1[$count]; # Array_1 will be used

if ( $count == 0 )
	{
	$REL_NAME = "RED";
	$REL_NUM = "2.5";
		}
		elsif ( $count == 2 )
			{
			$REL_NAME = "BLUE";
			$REL_NUM = "3.0";
			}
		    elsif ( $count == 3 )
			   {
			   $REL_NAME = "GREEN";
			   $REL_NUM = "3.5";
			   }

$BUILD_NUM	= $LINE_VAR;

# call some subds;
# do whatever;
} # will repeat fo each element of myArray_1


sub myArray_2_logic
{
my ($count) = @_;
$LINE_VAR = $myArray_2[$count]; # Array_2 will be used

if ( $count == 0 )
	{
	$REL_NAME = "GREEN";
	$REL_NUM = "3.5";
		}
		elsif ( $count == 2 )
			{
			$REL_NAME = "BLUE";
			$REL_NUM = "3.0";
			}
		    elsif ( $count == 3 )
			   {
			   $REL_NAME = "GREEN";
			   $REL_NUM = "3.5";
			   }

$BUILD_NUM	= $LINE_VAR;

# call some subds;
# do whatever;
} # will repeat fo each element of myArray_2

sub myArray_3_logic
{
my ($count) = @_;
$LINE_VAR = $myArray_3[$count]; # Array_3 will be used

if ( $count == 0 )
	{
	$REL_NAME = "GREEN";
	$REL_NUM = "3.5";
		}
		elsif ( $count == 2 )
			{
			$REL_NAME = "BLUE";
			$REL_NUM = "3.0";
			}
		    elsif ( $count == 3 )
			   {
			   $REL_NAME = "RED";
			   $REL_NUM = "2.5";
			   }
$BUILD_NUM	= $LINE_VAR;

# call some subds;
# do whatever;
} # will repeat fo each element of myArray_3

I'll get my coffe, try to simplify it more and will post a simple working script.

Thank you very much!
 
Once again:

My Goal is to simplify the script and being able to change array's sequence without changing other conditions.

(at this time conditions are based on the element's count)

Thanks again,
Zen20
 
This thread is closed. I will start a new question.

Thank you everybody!
 
this line looks wrong:

my ($count) = @_;

if you want the length of the array you need to remove the parenthesis:

my $count = @_;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
zen

It looks like you need to move your 'data' out to a configuration file that gets read by your build routine(s).

The software you are releasing, what is it written in? There are a number of language-specific tools explicitly targeted at automated build and deployment (Ant for Java, NAnt for .NET, Rake for Ruby etc.) and you might find that these are better suited to the task.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top