centricrot.blogg.se

Perl find word in file and print string
Perl find word in file and print string









The result of the comparison controls a while loop, which executes the block as long as the comparison is true. The guess is compared with the secret word using the ne operator, which returns true if the strings are not equal (this is the logical opposite of the eq operator). After the greeting the (non-Randal) person is asked (with another print) for the guess. What is the secret word? " įirst, we define the secret word by putting it into another scalar variable, $secretword. Print "Hello, $name!\n" # ordinary greeting Print "Hello, Randal! How good of you to be here!\n" First the program, and then an explanation: #!/usr/bin/perl -w For everyone except Randal, we'll have the program repeatedly ask for guesses until the person guesses properly. Well, now that we have the name, let's have the person running the program guess a secret word. Putting it all together, we get: #!/usr/bin/perl -w Now all we need to do is say Hello, followed by the value of the $name variable, which we can do in a shell-like fashion by embedding the variable inside the quoted string: print "Hello, $name!\n" Īs with the shell, if we want a dollar sign rather than a scalar variable reference, we can precede the dollar sign with a backslash. To get rid of that, we use the chomp function, which takes a scalar variable as its sole argument and removes the trailing newline (record separator), if present, from the string value of the variable: chomp ($name) The value of $name at this point has a terminating newline ( Randal comes in as Randal\n). This gives us the program: print "What is your name? " We assign this input to the $name variable. And the way to get a line from the terminal is with the construct, which (as we're using it here) grabs one line of input.

Perl find word in file and print string how to#

The previous program showed us how to prompt: use the print function. To do that, we need a way to prompt and a way to accept input. For now, assume that you can hold a single number or string (sequence of characters) in a scalar variable. We'll go into more detail in Chapter 2, Scalar Data, about what these variables can hold, and what you can do with them. For this program, we'll use the scalar variable $name to hold your name. One kind of place to hold values (like a name) is a scalar variable. To do this, we need a place to hold the name, a way to ask for the name, and a way to get a response. Let's have the program call you by your name. The Hello, world greeting is a touch cold and inflexible. As in C, all simple statements in Perl are terminated by a semicolon.ġ.5.2 Asking Questions and Remembering the Result The print statement is terminated by a semicolon ( ). Within this string, the character combination \n stands for a newline character. The built-in function print starts it off, and in this case has just one argument, a C-like text string. The second line is the entire executable part of this program. You should always develop your programs under -w.

perl find word in file and print string

This very important switch tells Perl to produce extra warning messages about potentially dangerous constructs.

perl find word in file and print string

Unlike all other comments in the program, the one on the first line is special: Perl looks at that line for any optional arguments. It's also a comment for Perl remember that a comment is anything from a pound sign to the end of that line, as in many interpreter programming languages. The first line is the incantation that says this is a Perl program. Here is your basic "Hello, world" program: #!/usr/bin/perl -w Let's look at a little program that actually does something.









Perl find word in file and print string