Array::Compare, compare(), full_compare()

本文介绍如何利用Perl脚本实现数组比较功能,适用于比较系统信息、远程文件列表、数据库内容变化等场景。通过示例代码,展示了如何获取并比较top命令输出的系统信息,以及如何通过差异输出来定位变化。

用于数组比较。

本例实现类似shellcommand - diff的功能。

如果我们要比较的不是文件,而是比如系统信息,远程文件列表,数据库内容变化等,这个模块会给我们提供方便灵活的操作。

#!/usr/bin/perl 

use Array::Compare; 

$comp = Array::Compare->new(WhiteSpace => 1); 
$cmd = "top -n1 | head -4"; 
@a1 = `$cmd`; 
@a2 = `$cmd`; 

@result = $comp->full_compare(@a1, @a2); 

foreach(@result) 
{ 
   print $_ + 1, "th line:\n"; 
   print "> $a1[$_]> $a2[$_]"; 
   print "-----\n"; 
} 
exit 0;


<dahua>dir e【cmlsh_completion】text = e filename_completion_matches: comp_state.is_initialized = 0, comp_state.current_text = (null),text = e filename_completion_function: text = e, state = 1 filename_completion_matches: state = 1 ❌ 没有补全项,返回 NULL <dahua>dir e【cmlsh_completion】text = e filename_completion_matches: comp_state.is_initialized = 0, comp_state.current_text = (null),text = e filename_completion_function: text = e, state = 0 parse_input_text: text=e → base_dir=, prefix=e filename_completion_function: current_base = , current_prefix = e filename_completion_function: full_path = etc/ filename_completion_matches: state = 0 filename_completion_matches: matches[0] = etc/, matches[1] = (null) tc/【cmlsh_completion】text = etc/ filename_completion_matches: comp_state.is_initialized = 0, comp_state.current_text = (null),text = etc/ filename_completion_function: text = etc/, state = 0 parse_input_text: text=etc/ → base_dir=etc/, prefix= filename_completion_function: current_base = etc/, current_prefix = filename_completion_function: full_path = etc/ssh/ filename_completion_matches: state = 0 filename_completion_matches: matches[0] = etc/ssh/, matches[1] = (null) ssh/【cmlsh_completion】text = etc/ssh/ filename_completion_matches: comp_state.is_initialized = 0, comp_state.current_text = (null),text = etc/ssh/ filename_completion_function: text = etc/ssh/, state = 0 parse_input_text: text=etc/ssh/ → base_dir=etc/ssh/, prefix= filename_completion_function: current_base = etc/ssh/, current_prefix = filename_completion_function: full_path = etc/ssh/ssh_host_ecdsa_key filename_completion_matches: state = 0 filename_completion_matches: matches[0] = etc/ssh/ssh_host_ecdsa_key, matches[1] = (null) ssh_host_ecdsa_key【cmlsh_completion】text = etc/ssh/ssh_host_ecdsa_key filename_completion_matches: comp_state.is_initialized = 0, comp_state.current_text = (null),text = etc/ssh/ssh_host_ecdsa_key filename_completion_function: text = etc/ssh/ssh_host_ecdsa_key, state = 0 parse_input_text: text=etc/ssh/ssh_host_ecdsa_key → base_dir=etc/ssh/, prefix=ssh_host_ecdsa_k Username: 还是会递归子目录
07-31
#include <stdio.h> #include <stdlib.h> #include "include/aip_common.h" #include "include/dynamic_array.h" #include "include/search_functions.h" #define BUFFER_SIZE 512 /*===================================================================================================================================*/ /* Function Name: v_s_main_print_int */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Prints an integer element from void pointer */ /* Arguments: VDP vdp_a_elem : Void pointer to integer element */ /* Return: void */ /*===================================================================================================================================*/ static void v_s_main_print_int(VDP vdp_a_elem) { printf("%d", *(S4 *)vdp_a_elem); } /*===================================================================================================================================*/ /* Function Name: v_s_main_print_float */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Prints a float element from void pointer */ /* Arguments: VDP vdp_a_elem : Void pointer to float element */ /* Return: void */ /*===================================================================================================================================*/ static void v_s_main_print_float(VDP vdp_a_elem) { printf("%.2f", *(float *)vdp_a_elem); } /*===================================================================================================================================*/ /* Function Name: v_s_main_print_char */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Prints a character element from void pointer */ /* Arguments: VDP vdp_a_elem : Void pointer to character element */ /* Return: void */ /*===================================================================================================================================*/ static void v_s_main_print_char(VDP vdp_a_elem) { printf("'%c'", *(S1 *)vdp_a_elem); } /*===================================================================================================================================*/ /* Function Name: v_s_main_print_array */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Prints contents of dynamic array */ /* Arguments: const ST_DA_ARRAY *stp_a_arr : Pointer to dynamic array structure */ /* void (*vfp_a_print_element)(VDP) : Function pointer for element printing */ /* Return: void */ /*===================================================================================================================================*/ static void v_s_main_print_array(const ST_DA_ARRAY *stp_a_arr, void (*vfp_a_print_element)(VDP)) { U4 u4_t_main_i; printf("["); for (u4_t_main_i = U4_MIN; u4_t_main_i < stp_a_arr->u4_s_size; u4_t_main_i++) { VDP vdp_t_main_elem = (U1 *)stp_a_arr->vdp_a_data + u4_t_main_i * stp_a_arr->u4_s_elem_size; vfp_a_print_element(vdp_t_main_elem); if (u4_t_main_i < stp_a_arr->u4_s_size - 1) { printf(", "); } } printf("]\n"); } /*===================================================================================================================================*/ /* Function Name: v_s_main_parse_int_array */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Parses integer input into dynamic array */ /* Arguments: ST_DA_ARRAY *stp_a_arr : Pointer to dynamic array structure */ /* Return: void */ /*===================================================================================================================================*/ static void v_s_main_parse_int_array(ST_DA_ARRAY *stp_a_arr) { U1 u1_t_main_buffer[BUFFER_SIZE]; S4 s4_t_main_value; U1 *u1p_t_main_token; printf("Enter integers separated by spaces: "); fgets((S1 *)u1_t_main_buffer, BUFFER_SIZE, stdin); u1p_t_main_token = (U1 *)strtok((S1 *)u1_t_main_buffer, " "); while (u1p_t_main_token != NULL) { s4_t_main_value = atoi((S1 *)u1p_t_main_token); v_g_da_push_back(stp_a_arr, &s4_t_main_value); u1p_t_main_token = (U1 *)strtok(NULL, " "); } } /*===================================================================================================================================*/ /* Function Name: v_s_main_parse_float_array */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Parses float input into dynamic array */ /* Arguments: ST_DA_ARRAY *stp_a_arr : Pointer to dynamic array structure */ /* Return: void */ /*===================================================================================================================================*/ static void v_s_main_parse_float_array(ST_DA_ARRAY *stp_a_arr) { U1 u1_t_main_buffer[BUFFER_SIZE]; float f_t_main_value; U1 *u1p_t_main_token; printf("Enter floating-point numbers separated by spaces: "); fgets((S1 *)u1_t_main_buffer, BUFFER_SIZE, stdin); u1p_t_main_token = (U1 *)strtok((S1 *)u1_t_main_buffer, " "); while (u1p_t_main_token != NULL) { f_t_main_value = atof((S1 *)u1p_t_main_token); v_g_da_push_back(stp_a_arr, &f_t_main_value); u1p_t_main_token = (U1 *)strtok(NULL, " "); } } /*===================================================================================================================================*/ /* Function Name: v_s_main_parse_char_array */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Parses character input into dynamic array */ /* Arguments: ST_DA_ARRAY *stp_a_arr : Pointer to dynamic array structure */ /* Return: void */ /*===================================================================================================================================*/ static void v_s_main_parse_char_array(ST_DA_ARRAY *stp_a_arr) { U1 u1_t_main_buffer[BUFFER_SIZE]; U4 u4_t_main_i; S1 s1_t_main_value; printf("Enter characters without spaces: "); fgets((S1 *)u1_t_main_buffer, BUFFER_SIZE, stdin); for (u4_t_main_i = U4_MIN; u4_t_main_i < BUFFER_SIZE; u4_t_main_i++) { s1_t_main_value = u1_t_main_buffer[u4_t_main_i]; if (s1_t_main_value == '\n' || s1_t_main_value == '\0') { break; } v_g_da_push_back(stp_a_arr, &s1_t_main_value); } } /*===================================================================================================================================*/ /* Function Name: main */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Main program for dynamic array operations */ /* Arguments: void */ /* Return: S4 : Exit status */ /*===================================================================================================================================*/ S4 main(void) { S4 s4_t_main_choice = S4_MIN; U1 u1_t_main_exit_flag = FALSE; while (!u1_t_main_exit_flag) { printf("\n===== Dynamic Array Program =====\n"); printf("1. Integer Sorting and Binary Search\n"); printf("2. Float Sorting and Binary Search\n"); printf("3. Character Sorting and Binary Search\n"); printf("4. Exit Program\n"); printf("Please select an option (1-4): "); scanf("%d", &s4_t_main_choice); getchar(); if (s4_t_main_choice == 4) { printf("Exiting program.\n"); u1_t_main_exit_flag = TRUE; continue; } ST_DA_ARRAY st_t_main_array; void (*vfp_t_main_print_element)(VDP) = NULL; S4 (*s4fp_t_main_compare)(const VDP, const VDP) = NULL; void (*vfp_t_main_parse_array)(ST_DA_ARRAY *) = NULL; U1 u1_t_main_found = FALSE; U1 u1_t_main_buffer[BUFFER_SIZE]; switch (s4_t_main_choice) { case 1: v_g_da_init(&st_t_main_array, sizeof(S4)); vfp_t_main_print_element = v_s_main_print_int; s4fp_t_main_compare = s4_g_comp_int; vfp_t_main_parse_array = v_s_main_parse_int_array; break; case 2: v_g_da_init(&st_t_main_array, sizeof(float)); vfp_t_main_print_element = v_s_main_print_float; s4fp_t_main_compare = s4_g_comp_float; vfp_t_main_parse_array = v_s_main_parse_float_array; break; case 3: v_g_da_init(&st_t_main_array, sizeof(S1)); vfp_t_main_print_element = v_s_main_print_char; s4fp_t_main_compare = s4_g_comp_char; vfp_t_main_parse_array = v_s_main_parse_char_array; break; default: printf("Invalid choice. Please select a valid option.\n"); continue; } vfp_t_main_parse_array(&st_t_main_array); printf("Original array: "); v_s_main_print_array(&st_t_main_array, vfp_t_main_print_element); printf("Sorting the array...\n"); v_g_da_bubble_sort(&st_t_main_array, s4fp_t_main_compare); printf("Sorted array: "); v_s_main_print_array(&st_t_main_array, vfp_t_main_print_element); printf("Enter the value to search: "); fgets((S1 *)u1_t_main_buffer, BUFFER_SIZE, stdin); if (s4_t_main_choice == 1) { S4 s4_t_main_target = atoi((S1 *)u1_t_main_buffer); u1_t_main_found = u4_g_search_binary(&st_t_main_array, &s4_t_main_target, s4fp_t_main_compare); } else if (s4_t_main_choice == 2) { float f_t_main_target = atof((S1 *)u1_t_main_buffer); u1_t_main_found = u4_g_search_binary(&st_t_main_array, &f_t_main_target, s4fp_t_main_compare); } else if (s4_t_main_choice == 3) { S1 st_t_main_target = u1_t_main_buffer[0]; u1_t_main_found = u4_g_search_binary(&st_t_main_array, &st_t_main_target, s4fp_t_main_compare); } if (u1_t_main_found) { printf("Value found in the array!\n"); } else { printf("Value not found in the array.\n"); } v_g_da_free(&st_t_main_array); } return 0; } 修改,增强健壮性,给出完整代码
09-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值