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


In reply to Using Menu and getting result by ChristieJS

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.