Beginner Need Help on perl input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Beginner Need Help on perl input
# 1  
Old 09-14-2011
Beginner Need Help on perl input

Hi, I am beginner perl input I just want to know if I want to make a program that let user input a function such as input " < value " List all files that are less than value The current directory is used. eg input " < 5" list all file that less than 5 in current directory until user input "quit"

how can I get sperate value from < in user input

so here my code for user input two sperate input in once line

Code:
#!/usr/bin/perl -w

print " Welcome to advance search function";

$func = "";

while (( $func ne "quit\n" ) && ( $func2 = "exit\n" ))
{
    print " Enter advance search function :";
    $func = <STDIN>; $func2 = <STDIN>;
    
    
}
print " return to main menu \n";

Thank

Last edited by pludi; 09-14-2011 at 08:36 PM..
# 2  
Old 09-14-2011
Quote:
Originally Posted by guidely
... list all file that less than 5 in current directory...
What does "a file that is less than 5" mean?

tyler_durden
# 3  
Old 09-14-2011
Mean the file that less than 5 byte will be display
# 4  
Old 09-15-2011
Quote:
Originally Posted by guidely
Mean the file that less than 5 byte will be display
Code:
$
$
$ # list the name and sizes of all text files in the current directory
$
$ wc *.txt
 0  0  0 a.txt
 1  1  3 b.txt
 1  1  5 c.txt
 1 11 51 d.txt
 3 13 59 total
$
$
$ # display the contents of the Perl program
$
$ cat -n search.pl
     1  #perl -w
     2  print "Enter expression: ";                              # prompt for user input
     3  chomp ($input = <STDIN>);                                # assign input to $input
     4  while ($input ne "quit") {                               # loop until user enters "quit"
     5    print "Expression to test: Filesize $input\n";         # print message
     6    print "It is TRUE for the following files:\n\n";       # print message
     7    foreach $file (glob "*.txt") {                         # loop through all text files in current dir
     8      $size = -s $file;                                    # find out the size of current file in loop
     9      $expr = "-s \"$file\" $input";                       # cook up the expression for testing filesize
    10      $output = eval $expr;                                # evaluate the expression
    11      die "Oops, something went wrong: $@" if $@;          # halt the program if something went wrong
    12      if ($output) {                                       # otherwise
    13        print "File Name = $file; File size = $size\n";    # print the file name if the expression
    14      }                                                    # could be evaluated successfully
    15    }                                                      # finish looping through all files
    16    print "\nEnter expression: ";                          # so prompt the user again
    17    chomp ($input = <STDIN>);                              # and set the input to $input
    18  }
$
$
$ # Now run the Perl program
$
$ perl search.pl
Enter expression: < 5
Expression to test: Filesize < 5
It is TRUE for the following files:

File Name = a.txt; File size = 0
File Name = b.txt; File size = 3

Enter expression: >= 0
Expression to test: Filesize >= 0
It is TRUE for the following files:

File Name = a.txt; File size = 0
File Name = b.txt; File size = 3
File Name = c.txt; File size = 5
File Name = d.txt; File size = 51

Enter expression: > 0
Expression to test: Filesize > 0
It is TRUE for the following files:

File Name = b.txt; File size = 3
File Name = c.txt; File size = 5
File Name = d.txt; File size = 51

Enter expression: != 5
Expression to test: Filesize != 5
It is TRUE for the following files:

File Name = a.txt; File size = 0
File Name = b.txt; File size = 3
File Name = d.txt; File size = 51

Enter expression: < 0
Expression to test: Filesize < 0
It is TRUE for the following files:


Enter expression: > 1000
Expression to test: Filesize > 1000
It is TRUE for the following files:


Enter expression: quit
$
$ # Another trial run
$
$ perl search.pl
Enter expression: < 50
Expression to test: Filesize < 50
It is TRUE for the following files:

File Name = a.txt; File size = 0
File Name = b.txt; File size = 3
File Name = c.txt; File size = 5

Enter expression: blah
Expression to test: Filesize blah
It is TRUE for the following files:

Bareword found where operator expected at (eval 10) line 1, near ""a.txt" blah"
        (Missing operator before blah?)
Oops, something went wrong: syntax error at (eval 10) line 2, near ""a.txt" blah
"
$
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 5  
Old 09-15-2011
Quote:
Originally Posted by durden_tyler
Code:
$
$
$ # list the name and sizes of all text files in the current directory
$
$ wc *.txt
 0  0  0 a.txt
 1  1  3 b.txt
 1  1  5 c.txt
 1 11 51 d.txt
 3 13 59 total
$
$
$ # display the contents of the Perl program
$
$ cat -n search.pl
     1  #perl -w
     2  print "Enter expression: ";                              # prompt for user input
     3  chomp ($input = <STDIN>);                                # assign input to $input
     4  while ($input ne "quit") {                               # loop until user enters "quit"
     5    print "Expression to test: Filesize $input\n";         # print message
     6    print "It is TRUE for the following files:\n\n";       # print message
     7    foreach $file (glob "*.txt") {                         # loop through all text files in current dir
     8      $size = -s $file;                                    # find out the size of current file in loop
     9      $expr = "-s \"$file\" $input";                       # cook up the expression for testing filesize
    10      $output = eval $expr;                                # evaluate the expression
    11      die "Oops, something went wrong: $@" if $@;          # halt the program if something went wrong
    12      if ($output) {                                       # otherwise
    13        print "File Name = $file; File size = $size\n";    # print the file name if the expression
    14      }                                                    # could be evaluated successfully
    15    }                                                      # finish looping through all files
    16    print "\nEnter expression: ";                          # so prompt the user again
    17    chomp ($input = <STDIN>);                              # and set the input to $input
    18  }
$
$
$ # Now run the Perl program
$
$ perl search.pl
Enter expression: < 5
Expression to test: Filesize < 5
It is TRUE for the following files:

File Name = a.txt; File size = 0
File Name = b.txt; File size = 3

Enter expression: >= 0
Expression to test: Filesize >= 0
It is TRUE for the following files:

File Name = a.txt; File size = 0
File Name = b.txt; File size = 3
File Name = c.txt; File size = 5
File Name = d.txt; File size = 51

Enter expression: > 0
Expression to test: Filesize > 0
It is TRUE for the following files:

File Name = b.txt; File size = 3
File Name = c.txt; File size = 5
File Name = d.txt; File size = 51

Enter expression: != 5
Expression to test: Filesize != 5
It is TRUE for the following files:

File Name = a.txt; File size = 0
File Name = b.txt; File size = 3
File Name = d.txt; File size = 51

Enter expression: < 0
Expression to test: Filesize < 0
It is TRUE for the following files:


Enter expression: > 1000
Expression to test: Filesize > 1000
It is TRUE for the following files:


Enter expression: quit
$
$ # Another trial run
$
$ perl search.pl
Enter expression: < 50
Expression to test: Filesize < 50
It is TRUE for the following files:

File Name = a.txt; File size = 0
File Name = b.txt; File size = 3
File Name = c.txt; File size = 5

Enter expression: blah
Expression to test: Filesize blah
It is TRUE for the following files:

Bareword found where operator expected at (eval 10) line 1, near ""a.txt" blah"
        (Missing operator before blah?)
Oops, something went wrong: syntax error at (eval 10) line 2, near ""a.txt" blah
"
$
$
$

tyler_durden
Thank you so much for your time, I own you one
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in parsing an input in perl

I am executing a command it is returning me something like this name ip port ------------------------------------ http-listener-1 * 6712 http-listener-2 * 8709 I have a subroutine getListenerName($porttobeChecked) This subroutine returns me the name of the listener if i pass a... (4 Replies)
Discussion started by: javaholics
4 Replies

2. Shell Programming and Scripting

silent Input in PERL

Hello Experts, I am learning perl. I know ksh/bash/csh... In ksh I use to do this way... to read user input in silent mode so that nothing returns on the screen. stty -echo read -r pswd stty echo Please let me know the way in perl how to do it. Here are my OS and Perl Details... ... (3 Replies)
Discussion started by: explorer007
3 Replies

3. Shell Programming and Scripting

user input in perl?

Please tell me how to write a perl script that asks the user to enter words and that passes them to a variable. In bash, the "read" command would achieve such user interaction. #!/bin/bash read -p "Enter files: " vFiles However, I am looking for perl version of something equivalent... (2 Replies)
Discussion started by: LessNux
2 Replies

4. Shell Programming and Scripting

User input - Perl need Help

If I want all user input to start with " : " if not display error or what I asking is how to do if statement that control a first letter of string that we want to start with. and not worry about the rest Thank (1 Reply)
Discussion started by: guidely
1 Replies

5. Shell Programming and Scripting

Beginner Perl help Please

Hi I am trying to generate random names using perl script. First let me post my code #!/usr/bin/perl my $name; my @charset = (('A'..'Z'), ('a'..'z')); my $range = $#charset + 1; print "Enter the limit\n"; $lim = <STDIN>; until ($lim == 0){ for (1..8) { $name .=... (2 Replies)
Discussion started by: srijith
2 Replies

6. Shell Programming and Scripting

Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable Something like:perl -p -e 's/$shell_srch/$shell_replace/g' input.txt I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.) I have searched and found... (4 Replies)
Discussion started by: alex_5161
4 Replies

7. UNIX for Dummies Questions & Answers

(Beginner) Run c++ .exe with input to file

Hi, I've got this requirement for my homework assignment, but I'm not sure how to meet it: In the comamnd line, I need to type $ <exec-file> <input> <output_file_name> Like: test 1+2 out.txt Which should execute test.exe passing in 1+2 and directing output to out.txt. I know how... (1 Reply)
Discussion started by: JustinT
1 Replies

8. Shell Programming and Scripting

Beginner for Perl

Hi, I'm new in Perl. Can anyone recommend me any e-book which is good for beginners? Thanks. (3 Replies)
Discussion started by: raul15791
3 Replies

9. Shell Programming and Scripting

[Perl] Silent Input

I would like to use the WWW::Mechanize module to access a webpage that is password-protected. I was wondering if there was a way to make the input silent when asked from the script. For example: What is your password: <password> Where <password> is where you put your password, but is silent... (2 Replies)
Discussion started by: eightysix
2 Replies

10. Shell Programming and Scripting

getting input from perl

Hi, i have just tried something in perl #!/path/to/perl print "login: "; $login = <STDIN>; print "\npassword: "; $password = <STDIN>; print "Username=$login\n"; print "Password=$password\n"; And it doesnt work, anyone know how i can get more than one line? Cheers, Elfyn (2 Replies)
Discussion started by: emcb
2 Replies
Login or Register to Ask a Question

Featured Tech Videos