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.
I got this example Perl code off of oopweb.com but when I try and run it I get an error:
"Number found where operator expected at ./testperl line 8, near "1"
(Missing semicolon on previous line?)
syntax error at ./testperl line 8, near "1"
Execution of ./testperl aborted due to compilation errors."
Here is the code.
Code:
print "Please enter your name:\n";
$name = <>;
chomp($name);
$fl = lc(substr($name
0
1));
if (($fl eq "a")||($fl eq "b")||($fl eq "c"))
{
print "Your name starts with one of the " .
"first three letters of the ABC.\n";
}
else
{
print "Your name does not start with one of the " .
"first three letters of the ABC.\n";
}
Also, I've been looking at the perldoc for lc and for subsrt but I don't quite understand what is going on with them. And I definitely don't understand what they are doing together in the above example.
substr creates another substring of the $name variable with an offset and the length of characters from the offset.
so the substr expression used in this starts at offset 0 which is in front of the 1st letter and takes 1 which is the 1st letter.
Sorry if I confused you, in short the expression takes the first letter of the name that is typed in, turns it to lowercase and checks to see if it is a,b, or c.
Thanks for explaining it. heh Unfortunately it looks like it won't work for what I'm trying to do.
Basically I'm trying to search through an array using input from the user. So it would be like, take first search term, search array, store answer, take second term, search array, store answer, take third search term, search array, store answer, display matches.
I found this code that says it's for searching array's but I don't fully understand this one either
Code:
sub search {
my ($foo, $test) = @_;
for my $bar (@$foo) {
if ($foo eq $test) {
return 1;
}
}
return 0;
}
$found = search(\@array, $string);
@query;
@arraytosearch;
@matches;
foreach (<>){
push @query, $_;
}
chomp @query;
for (@query){
for (@arraytosearch){
if ($_ eq $query){
push @matches, $_;
}}}
print "@matches\n";
Edit this to suit your needs, but it's the jist of what you asked for
What that one will do is search @array for $string and just return a true/false (1 or 0). That seems like it would be slow (code-wise, I don't know about compiler) to check through many search terms. Personally, I tend to side for scalability over speed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.