Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Using Menu and getting result

by ChristieJS (Novice)
on Jun 18, 2014 at 11:29 UTC ( #1090298=perlquestion: print w/ replies, xml ) Need Help??
ChristieJS has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I have a task, which is to read a file and print the lines with number prefix, line by line, Then let the user select a choice, and print the value after "=" into a variable.

Below is the code

use strict; use warnings; @agents = $session->GetAllAgents(); $i=1; $inputfile = "Agents.txt"; open (INPUTF, '>', $inputfile) || die("FATAL ERROR: Cannot open output + file.\n"); foreach $agent($i) { foreach $agent (@agents) { my $num = $i ++; print INPUTF "$num "; print INPUTF "Agent Name = ". $agent->Name() . "\n"; print "$num. "; print "Agent Name = " . $agent->Name() . "\n";# } } close(INPUTF); print '*' x 80 ; print "\n"; print " Select Agent to Export:"; chomp ($choice = <STDIN>); open(inputfile) or die("FATAL ERROR: Cannot open input file.\n"); my @lines = <inputfile>; $fVal = ""; $line = ""; foreach $line (@lines) { if ( ($fVal ne "" ) && ( lc $fVal eq lc $choice ) ) { if($line =~ /$choice/) { $Agtname = "$line"; print $Agtname; my @values = split('=', $Agtname); $value = @values; $val = $values[2]; #$val =~ s/^\s+|\s+$//g; #$fVal = substr $val, 15; #print $fVal; #$Agtname = $fVal; #print $Agtname; last; } } } close(inputfile);

And the output would be like below

1. Agent Name = 153002_nlidmovwb01_apa_1

2. Agent Name = 153002_nwidmovap01_iis_1

3. Agent Name = etaadmin

4. Agent Name = migdemo-agent

5. Agent Name = migdemothree-agent

6. Agent Name = wa_153002_idmap_apa_1

7. Agent Name = 35-000cfc42-7389-1399-aa09-3853873c308d

8. Agent Name = test wokspace agent

********************************************************************************

Select Agent to Export:

if i choose 3, i expect the third line to print, split and get the value after "=" only

can somebody help me here and tell me whats the mistake i do, and how to correct it

Comment on Using Menu and getting result
Download Code
Re: Using Menu and getting result
by marto (Chancellor) on Jun 18, 2014 at 11:33 UTC

    "can somebody help me here and tell me whats the mistake i do, and how to correct it"

    Did you run this code before posting it? Without bothering to look any further I see Perl give you several pointers to problems:

    D:\>perl derp.pl Unquoted string "inputfile" may clash with future reserved word at der +p.pl line 24. Unquoted string "inputfile" may clash with future reserved word at der +p.pl line 48. Global symbol "@agents" requires explicit package name at derp.pl line + 4. Global symbol "$session" requires explicit package name at derp.pl lin +e 4. Global symbol "$i" requires explicit package name at derp.pl line 5. Global symbol "$inputfile" requires explicit package name at derp.pl l +ine 6. Global symbol "$inputfile" requires explicit package name at derp.pl l +ine 7. Global symbol "$agent" requires explicit package name at derp.pl line +9. Global symbol "$i" requires explicit package name at derp.pl line 9. Global symbol "$agent" requires explicit package name at derp.pl line +11. Global symbol "@agents" requires explicit package name at derp.pl line + 11. Global symbol "$i" requires explicit package name at derp.pl line 13. Global symbol "$agent" requires explicit package name at derp.pl line +15. Global symbol "$agent" requires explicit package name at derp.pl line +17. Global symbol "$choice" requires explicit package name at derp.pl line + 23. Global symbol "$fVal" requires explicit package name at derp.pl line 2 +6. Global symbol "$line" requires explicit package name at derp.pl line 2 +7. Global symbol "$line" requires explicit package name at derp.pl line 2 +8. Global symbol "$fVal" requires explicit package name at derp.pl line 3 +0. Global symbol "$fVal" requires explicit package name at derp.pl line 3 +0. Global symbol "$choice" requires explicit package name at derp.pl line + 30. Global symbol "$line" requires explicit package name at derp.pl line 3 +2. Global symbol "$choice" requires explicit package name at derp.pl line + 32. Global symbol "$Agtname" requires explicit package name at derp.pl lin +e 34. Global symbol "$line" requires explicit package name at derp.pl line 3 +4. Global symbol "$Agtname" requires explicit package name at derp.pl lin +e 35. Global symbol "$Agtname" requires explicit package name at derp.pl lin +e 36. Global symbol "$value" requires explicit package name at derp.pl line +37. Global symbol "$val" requires explicit package name at derp.pl line 38 +. Execution of derp.pl aborted due to compilation errors.

    Consider fixing these problems first.

Re: Using Menu and getting result
by Laurent_R (Parson) on Jun 18, 2014 at 17:54 UTC
    You state what you want to do and show your code, which is good, but you did not state what your problem is and what you need help for. Are we supposed to guess?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1090298]
Approved by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-09-17 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (100 votes), past polls