[SOLVED] perl script cannot match the word with [ or ] character.
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
@pin[$i] is an array, I am going to match the element in that array.
therefore, i dont thinkg "if( $temp_pin =~ m/^@pin\[$i\]$/ )" is a right way.
any opinion?
part of my code:
@split2 = split(" ", @file2[$line2]);
$temp_pin = shift(@split2)
if( $temp_pin =~ m/^@pin[$i]$/ )
With '[]' as part of the value you are trying to match , only the string comparison works.
Without the '[]' both work.
Note that the string comparison works with or without '[]' in the compared values.
Your Perl syntax generally bothers me:
Code:
# this is a Perl array
@pin;
# this is the 'ith' element of the array
# a single value is always a scalar ($var), even if its an array element
# or a hash key/val eg $hash{$key} = $val, where %hash is the hash.
$pin[$i];
# cast an array ref ($i) stored inside an arr element ( $arr2[$i] )to an array
@arr1 = @{$arr2[$i]}
1. eq for strings
2. == for nums (ints )
for floats you need to worry about rounding etc
Code:
# equal(NUM1, NUM2, ACCURACY) : returns true if NUM1 and NUM2 are
# equal to ACCURACY number of decimal places
sub equal {
my ($A, $B, $dp) = @_;
return sprintf("%.${dp}g", $A) eq sprintf("%.${dp}g", $B);
}
3. only use regex if you want to match a (sub-)pattern rather than a complete value
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.