Visit Our UNIX and Linux User Community


perl script :making array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl script :making array
# 1  
Old 03-23-2012
perl script :making array

Hi ,

i have a perl script which takes 'csv; file as input parameter and performs some tasks.
CSV file format :
MAGENTF,AGENTF8,mahfr001,tksfr01,. ./.profile 1>/dev/null 2>&1;ps -fe|grep 'gn1avm_agent -n AGENTF8'|grep gn1avm_agent|tr -s '[ ]' '[ ]'|cut -d ' ' -f 3|xargs kill -9,,. ./.profile 1>/dev/null 2>&1;gn1avm_runproc_Ksh -e OLC1AVMA -n AGENTF8 &,,ps -fu \$LOGNAME | grep 'gn1avm_agent -n AGENTF8$'|grep -v grep

here it takes first column as GROUP.

Code:
output :-
script.pl file.csv
it will give a list (based on first column i mentioned above as GROUP)
        0) MAGENTF
        1) MAGENTR
        2) MCDR

but this script can select only one option and will proceed with its given action. if i select opyions 1 and then 2 , it will take all daemons mentioned under group 2 (MCDR). i want it to handle more than 1 option (handle more than 1 GROUP). so if i select 1 and 2 , it can take both group daemons and perform its action given on next page.

this perl script is making 2D array , part of code is below:-
Code:
$CHOSENAPP=();
%CHOOSEN_LOGICIAL_NAME_Hash=();
$CHOSENACTION=();

%Unique_APPS_Hash=();
%Unique_NAMES_Hash=();

#### OPEN INPUT CONFIG  FILE  AND READ INTO 2 DIMENSIONAL ARRAY #################
sub read_config
{
	open(FILE_IN, "<$DIR/$FILE" ) || &File_Error;

	$X=0; #initialize count of lines in file and array
	while ( <FILE_IN> )
	{
		if ($_ =~ /^#/)
		{ if ($REPORT>=1){print "\nIGNORED COMMENT: $_\n";} }
		else
		{
			chomp($_); # remove xtra return character at end of line
			@line=split(/,/, $_); #split line on comma
			for $Y ( 0 .. $#line ) # number of items in array as split by comma
 			{
				$ConfigFileArray[$X][$Y]=@line[$Y]; # assign item to 2D array
 				if ($REPORT>=1){print " :$ConfigFileArray[$X][$Y]: ";}
			}
			$X++; # increment count of lines
			if ($REPORT>=1){print "\n";}
		}
	}
	close(FILE_IN);
}
#### END OPEN INPUT CONFIG  FILE  AND READ INTO 2 DIMENSIONAL ARRAY #################



### report the contents of 2D array ConfigFileArray ###
sub report_array_contents
{
	print "ARRAY CONTAINS:\n";
	for $X ( 0 .. $#ConfigFileArray ) { for $Y ( 0 .. $#{$ConfigFileArray[$X]} ) { print " :$ConfigFileArray[$X][$Y]: "; } print "\n"; }
}
### END report the contents of 2D array ConfigFileArray ###



##### Make unique list of APPS and LOGICIAL NAMES, store them in  HASHs ####
sub make_unique
{
	for $X ( 0 .. $#ConfigFileArray)
	{
		$Unique_APPS_Hash{$ConfigFileArray[$X][0]}++;
		$Unique_NAMES_Hash{$ConfigFileArray[$X][1]}++;if ($Unique_NAMES_Hash{$ConfigFileArray[$X][1]} > 1){print "LOGICIAL NAMES MUST BE UNIQUE< PLEASE CHECK THE CONFIG FILE! EXITING NOW\n";exit 1}
	}
	if ($DEBUG>=5)
	{
		print "APPS hash\n"; foreach $key (keys %Unique_APPS_Hash){print  "$key $Unique_APPS_Hash{$key}\n";}
		print "NAME hash\n"; foreach $key (keys %Unique_NAMES_Hash){print  "$key $Unique_NAMES_Hash{$key}\n";}
		print "\n\n";
	}
}
##### END Make uniques list in HASH of APPS and LOGICIAL NAMES ####
#### Menu to Stop or Start ###
sub action_menu
{
	@ACTION=('','STOP','START','CHECK');
	while ( $MYACTION eq '' )
	{
		menu_header();
		print "Screen 3 of 4\n";

		print "Choose the ACTION: \n\n";
		print "\tE) EXIT\n";
		print "\tC) CLEAR chosen ACTION\n";
		print "\tD) I'm DONE chosing, proceed to next menu\n\n";
		print "\t1) STOP\n\t2) START\n\t3) CHECK\n";
		sysread(STDIN,$MYACTION,2); chomp ($MYACTION);
		print $clear_string;	

		if ($MYACTION =~ /^[eE]$/) {action_info ("EXIT");exit 1;}
		elsif ($MYACTION =~ /^[1-3]$/) { action_info ($ACTION[$MYACTION]);$CHOSENACTION=$ACTION[$MYACTION];$MYACTION='';}
		elsif ($MYACTION =~ '^[cC]$') { action_info ("CLEAR"); $CHOSENACTION=(); $MYACTION=''; }
		elsif ($MYACTION =~ '^[dD]$') {action_info ("DONE");if (! $CHOSENACTION){print "\n\nERROR: you must choose an ACTION to proceed\n\n";$MYACTION='';}}
		else {action_info ("Invalid choice");$MYACTION='';}
	}
}

does it help to understand ?
i have no perl idea. let me know if smoething more is required .
# 2  
Old 03-23-2012
Quote:
Originally Posted by deepakiniimt
...
Code:
output :-
script.pl file.csv
it will give a list (based on first column i mentioned above as GROUP)
        0) MAGENTF
        1) MAGENTR
        2) MCDR

but this script can select only one option and will proceed with its given action. if i select opyions 1 and then 2 , it will take all daemons mentioned under group 2 (MCDR).
...
It might be helpful if you post the part of the script that shows the menu above, and processes the user input thereafter.

tyler_durden
# 3  
Old 03-24-2012
here is that:-
Code:
while ( $MYNAME eq '' )
	{
		menu_header();
		print "Screen 2 of 4\n";
		print "Choose the LOGICIAL ENVIRONMENT to work on: \n\n";
		@Unique_NAMES_Array=();
		for $X ( 0 .. $#ConfigFileArray) # make array of logicial names based on APP choice
		{
			if ($ConfigFileArray[$X][0] eq $CHOSENAPP)
			{ push (@Unique_NAMES_Array,$ConfigFileArray[$X][1]);}
		}
	
		print "\tE) EXIT\n";
		print "\tA) ALL envs listed below\n";
		print "\tC) CLEAR chosen envs\n";
		print "\tD) I'm DONE chosing, proceed to next menu\n\n";
		for $Z ( 0 .. $#Unique_NAMES_Array)
		{
			print "\t$Z) $Unique_NAMES_Array[$Z]\n";
		}
	
		sysread(STDIN,$MYNAME,3); chomp ($MYNAME);
		
		print $clear_string;	
		if ($MYNAME =~ '^[eE]$') {action_info ("EXIT");exit 1;}
		elsif (($MYNAME =~ /^[0-9]*$/) && ($MYNAME <= $#Unique_NAMES_Array)) { action_info ($Unique_NAMES_Array[$MYNAME]); $CHOOSEN_LOGICIAL_NAME_Hash{$Unique_NAMES_Array[$MYNAME]}++; $MYNAME=''; }	
		elsif ($MYNAME =~ '^[cC]$') { action_info ("CLEAR"); delete @CHOOSEN_LOGICIAL_NAME_Hash {keys %CHOOSEN_LOGICIAL_NAME_Hash}; $MYNAME=''; }
		elsif ($MYNAME =~ '^[aA]$') { action_info ("ALL"); foreach $item(@Unique_NAMES_Array) { $CHOOSEN_LOGICIAL_NAME_Hash{$item}++; }; $MYNAME=''; }
		elsif ($MYNAME =~ '^[dD]$') {action_info ("DONE");if (! %CHOOSEN_LOGICIAL_NAME_Hash){print "\n\nERROR: you must choose an ENV to proceed\n\n";$MYNAME='';}}
		else {action_info ("Invalid choice");$MYNAME='';}
	}
}


#### get the commands from the array ####
sub get_commands
{
	# copy for each env from Config file array the commands to use for childs process into new 2 D array
	foreach $env_item (keys %CHOOSEN_LOGICIAL_NAME_Hash)
	{
		if ($REPORT >=  1){print " $env_item\n ";}
		for $X ( 0 .. $#ConfigFileArray ) 
		{ 
			if ( ($ConfigFileArray[$X][1] eq $env_item) && ($ConfigFileArray[$X][0] eq $CHOSENAPP) )
			{
				if ($CHOSENACTION eq 'STOP')
				{ push (@commands_array,[$env_item,$ConfigFileArray[$X][2],$ConfigFileArray[$X][3],$ConfigFileArray[$X][4],$ConfigFileArray[$X][5]]); }
				elsif ($CHOSENACTION eq 'START')
				{ push (@commands_array,[$env_item,$ConfigFileArray[$X][2],$ConfigFileArray[$X][3],$ConfigFileArray[$X][6],$ConfigFileArray[$X][7]]); }
				elsif ($CHOSENACTION eq 'CHECK')
				{ push (@commands_array,[$env_item,$ConfigFileArray[$X][2],$ConfigFileArray[$X][3],$ConfigFileArray[$X][8],'']); }
			}
		} 
	}

	if ($DEBUG >=  2){
		for $X ( 0 .. $#commands_array ) 
		{print "$commands_array[$X][0] $commands_array[$x][1] $commands_array[$X][2] $commands_array[$X][3] $commands_array[$X][4]\n";}
		}
}

#### FORK the child processes and run the commands ###
sub fork_child_action
{
	if ($REPORT >=  1){print "\n\nForking the commands now....\n";}
	for $X ( 0 .. $#commands_array ) 
	{
		if ($cpid != 0)#this prevents endless loop
		{
			$cpid=fork(); # fork a new process identical to this one, and start eexcuting from here
			
			if ($cpid < 0) # error
			{ 
				print "ERROR: Unable to Fork! exit 1\n"; exit 1;
			}
			elsif ($cpid > 0) # fork returns the new process id of the child, the parent knows the childs pid now
			{ 
				if ($REPORT >=  1){print " $env_item childs process id is $cpid\n"; }

				#PUT my process ID into the array
				$commands_array[$X][5]=$cpid;
			}
			elsif ($cpid == 0) # in the child process, the pid is 0, so i'm a child, continue to process
			{
				#GET the valuse from array
				$env=$commands_array[$X][0];
				$host=$commands_array[$X][1];
				$wrk_account=$commands_array[$X][2];
				$command1=$commands_array[$X][3];
				$command2=$commands_array[$X][4];

				if ($DEBUG >=  2){print "$env CHILD FORKING HERE\n"; print "FORK COMMANDS: $env $host $wrk_account $command1 $command2\n";}
				
				open(FILEOUT, ">>".$LOG."/Emergency_".$env."_".$host."_".$wrk_account.".log") || die("Could not open log file for output");
				$TIMESTAMP= getTimeStamp();
				print FILEOUT "BEGIN $CHOSENACTION $TIMESTAMP $env $host $wrk_account $command1, $command2\n";

				if ( $command1 )
				{
				
					if ($REPORT >=  1){print "Issuing command1 $command1  TO $env $host $wrk_account \n";}
					
					open (STDOUT, ">>".$LOG."/Emergency_".$env."_".$host."_".$wrk_account.".stdout.log") || die("Could not open log file for output");
					open (STDERR, ">>".$LOG."/Emergency_".$env."_".$host."_".$wrk_account.".stdout.log") || die("Could not open log file for output");
					system ("ssh \-o \"StrictHostKeyChecking=no\" \-o 'BatchMode yes' \-q $wrk_account\@$host \"$command1\"") ;
					$exit_status=$?;
					close (STDOUT);
					close (STDERR);

# 4  
Old 03-26-2012
anyone can help ?

Previous Thread | Next Thread
Test Your Knowledge in Computers #577
Difficulty: Medium
If a language offers automated memory management, it is not possible to have memory leaks and other memory allocation issues.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

Making an array of 2D arrays in C

I hate I'm asking for help again. Unfortunately it seems there just aren't any links I can find on making an array that holds a bunch of two dimensional arrays. Maybe my google-fu is lacking. Basically I have a header file like this: #define MATRIX 10 int white_l1; int white_l2; int... (2 Replies)
Discussion started by: Azrael
2 Replies

2. UNIX for Advanced & Expert Users

Help in perl script Array.

hi Team, i need a help in perl , i need to get values(10 rows +) from perl GUI and insert those values into oracle table. am trying to achive this in Perl array, can you please help me on this. thanks senthil (1 Reply)
Discussion started by: senkerth
1 Replies

3. Shell Programming and Scripting

Number of arguments to array - Perl Script

I have the following proc. proc get_add {arg1 arg2 arg3 arg4 arg 5 .. ... arg N } { } i need to count the number of arguments and also i need those arguments stored in an array. please help out ---------- Post updated at 06:33 PM ---------- Previous update was at 05:30 PM ---------- ... (1 Reply)
Discussion started by: Syed Imran
1 Replies

4. Shell Programming and Scripting

Making a perl script executable

Hello, I have a perl program called snp_hwe.pl I have another program called hwe_test.run which contains the following: for file in *.inp do cp $file genotype_counts_c.txt ./snp_hwe.exe > $file'.res' done I want to change my perl program to an executable program while changing... (3 Replies)
Discussion started by: Homa
3 Replies

5. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

6. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

7. Shell Programming and Scripting

Making array of string with bash

in.txt libgstreamer gstreamer-0_10 gstreamer-0_10-plugins-good gstreamer-0_10-plugins-base Output should be: libgstreamer gstreamer-0_10 gstreamer-0_10-plugins-good gstreamer0_10-plugins-base Then: #!/bin/sh v=(libgstreamer gstreamer-0_10 gstreamer-0_10-plugins-good... (5 Replies)
Discussion started by: cola
5 Replies

8. Shell Programming and Scripting

How to export an array to perl script?

hi all...... i want to use an array ,declared in bash, in embedded perl script. is there any way to export whole array so that i can use it '$ENV{}' or something.. thanx in advance!! regards, prayush (1 Reply)
Discussion started by: tprayush
1 Replies

9. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

10. Shell Programming and Scripting

split and making an array inside another array

I want to run an awk split on a value that has been pushed through an array and I was wondering what the syntax should be?? e.g. running time strings through an array and trying to examine just minutes: 12:25:30 10:15:13 08:55:23 awk ' NR==FNR{ ... (2 Replies)
Discussion started by: dcfargo
2 Replies

Featured Tech Videos