ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
open(TEMP, "temp_file"); # this file is nothing but a list of filenames, one name in each line.
@arr = <TEMP>;
$temp = "@arr";
$line = "M1BRMA00"; # this line exists in "temp_file"
if ($temp =~ /substr($line, 0, 5)/)
{
print "found";
}
else
{
print "not found";
}
surprisingly(to me, that is) it printed "not found".
but when i assign the 'substr' part to another string, as in this prog,
Code:
open(TEMP, "temp_file");
@arr = <TEMP>;
$temp = "@arr";
$line = "M1BRMA00"; # this line exists in "temp_file"
$tempstr = substr($line, 0, 5);
if ($temp =~ /$tempstr/)
{
print "found";
}
else
{
print "not found";
}
it prints "found" which is correct. why? since 'substr' returns a subset of the target string, what is the difference between the two programs?
the difference is in the regular expression -- check your syntax on how to construct one -- you cannot just throw code in a regex all willy-nilly like and expect it to work....
--Regular Expressions have their own syntax, which differs from that of perl!
more:
i'm not really looking to trace through your code there, but why aren't you using a 'foreach'... seems to me you're lucky to be getting a found -- $temp = "@arr"??
maybe i just haven't written any perl in a while, but i would make it something like:
--yup, a quick modification and run, and that there is some good code!
[edit] add a counter to that, or something to not print every line... i ran this on a 9-line file, and it printed an accurate 'found' on line 8 of the result... there will be one line printed for every line in your file without any modification...[/edit]
thanks for ur replies secesh. yeah, the '(' and ')' is the problem, right? because it is part of regex syntax. stupid of me to ask the question in the first place. but how do i include a function in a regex? or pls tell me where i can read up on it.
thanks.
regexes can be very complex -- i have never really gone that deep with them... start with google - there's tons of documention on them... sorry i can't help more.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.