Can you add <code>...</code> tags around the input data from results.bat? That would be very helpful. I cannot tell what goes on each input line.

Modify my DATA section below with the correct formatting.
I made some assumptions about what the data looked like. I took out all of these chomp() statements as they are not necessary. Note that .* in a regex matches all characters except new line's. Also note that chomp @lines; is actually a foreach loop - don't do that if you are going to process each line individually again.

#!/usr/bin/perl use strict; use warnings; my ($sname,$result); #my $cmd ="results.bat"; my @lines = <DATA>; #print "@lines\n"; #chomp (@lines); foreach my $line(@lines){ # chomp ($line); if ($line =~ m/Student\sName\s+=\s(.*)/i){ # chomp ($1); $sname =$1; print "$sname "; #changed } if ($line =~ m/Exam\sStatus\s+=\s(.*)/i){ # chomp ($1); $result =$1; print "$result "; #changed } } print "\n"; =Prints: Harry PASSED Mike PASSED Tom PASSED =cut __DATA__ Student Name = Harry Student Code = student_id_1 Exam Status = PASSED ------------------------------------- Student Name = Mike Student Code = student_id_2 Exam Status = PASSED ------------------------------------- Student Name = Tom Student Code = student_id_3 Exam Status = PASSED -------------------------------------
I just made a small change to your print statement. I would have some other suggestions for you, but let's get the problem statement completely defined first. Your code although somewhat awkward basically "works".

Update:
I noticed that my DATA segment has some trailing spaces. That is probably not true of your actual input file. However a slight regex modification can handle this situation - a "minimal match" upon (.*) that allows for trailing blanks to be deleted. Here is some code for your consideration...

#!/usr/bin/perl use strict; use warnings; while (my $line = <DATA>) { print "$1 " if $line =~ m/Student Name\s*=\s*(.*?)\s*$/; #deletes t +railing spaces print "$1 " if $line =~ m/Exam Status\s*=\s*(.*?)\s*$/; #deletes t +railing spaces } print "\n"; =Prints: Harry PASSED Mike PASSED Tom PASSED Bob Smith FAILED =cut __DATA__ Student Name = Harry Student Code = student_id_1 Exam Status = PASSED ------------------------------------- Student Name = Mike Student Code = student_id_2 Exam Status = PASSED ------------------------------------- Student Name = Tom Student Code = student_id_3 Exam Status = PASSED ------------------------------------- Student Name = Bob Smith Student Code = student_id_4 Exam Status=FAILED
I can tell that you are very new to Perl. That is fine. And this is a good place to go. There is a limit to what can be conveyed in a single thread. If you read and learn more about chomp then I consider this instructional exercise a success.

In reply to Re: Print the output results of matching line horizontally by Marshall
in thread Print the output results of matching line horizontally by sreek3502

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.