LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2004, 03:42 PM   #1
Benr
LQ Newbie
 
Registered: Jan 2004
Posts: 12

Rep: Reputation: 0
Perl: What's this mean? (Code sample)


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.
 
Old 10-20-2004, 04:27 PM   #2
dannyp
LQ Newbie
 
Registered: Mar 2004
Location: Philadelphia, PA
Posts: 14

Rep: Reputation: 0
lc changes the letters to lowercase.

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.
 
Old 10-20-2004, 04:52 PM   #3
Benr
LQ Newbie
 
Registered: Jan 2004
Posts: 12

Original Poster
Rep: Reputation: 0
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);
 
Old 10-20-2004, 09:14 PM   #4
Chrax
Member
 
Registered: Apr 2004
Distribution: Dapper
Posts: 167

Rep: Reputation: 31
my suggestion:

Code:
@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.
 
Old 10-21-2004, 09:54 PM   #5
Benr
LQ Newbie
 
Registered: Jan 2004
Posts: 12

Original Poster
Rep: Reputation: 0
Hey, thanks for the reply. I've got my script to about where I want it, I can't seem to figure out how to make perl stop taking input though. heh

How can I tell perl that a blank return or something like it that that means stop and move on to the next part?
 
Old 10-23-2004, 07:20 AM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
From the Chrax's code, try change :
Code:
foreach (<> ){
    push @query, $_;
}
to :
Code:
while (<> ) {
    last if /^$/;
    push @query, $_;
}
So input stop when it meet a blank line ( /^$/ )
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
system() and execl anyone got any sample code please? twirl Programming 2 09-08-2005 02:01 AM
Using sample C++ code from a book...won't compile Baix Programming 4 03-05-2005 02:04 PM
Open Firmware code for booting OS from SATA : sample code available somewhere ? drsparikh Linux - Hardware 0 03-12-2004 11:16 AM
Sample code to build a window with slider bar? NewComer Programming 3 02-12-2004 01:34 PM
Any sample code of how to use Rand_seed() cybercop12us Programming 0 04-03-2003 07:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration