CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner: Re: [mar85] Need Help in Report File Handling: Edit Log


Apr 9, 2013, 11:16 PM

Views: 750
Re: [mar85] Need Help in Report File Handling

If your list.txt file is as shown, consider the following:

use strict; 
use warnings;

my @fruit;
local $/ = '';

while (<>) {
@fruit = /FRUITS (.+)/g if /Combined/;

print "Fruit name: $fruit[0 + $_] $fruit[2 + $_] -delivered\n" for 0 .. 1;

Usage: perl list.txt >list_arranged.txt


Fruit name: orange[0]/apples[1]/mango[3] orange.0/apples.1/mango.3 -delivered 
Fruit name: grape[0]/cherry[2]/papaya[3] grape.0/cherry.2/papaya.3 -delivered

The local $/ = '' notation sets the record separator to "paragraph mode," where blank lines separate paragraphs. Thus, Group #1, Group #2 and Groups Combined will be read in one chunk. The regex /Combined/ is attempting to match the last group. When matched, the regex /FRUITS (.+)/g captures the last part of each line that's past "FRUITS ", and those parts are passed to @fruit.

The last step prints the newly-arranged data.

Hope this helps!

(This post was edited by Kenosis on Apr 10, 2013, 8:47 AM)

Edit Log:
Post edited by Kenosis (User) on Apr 9, 2013, 11:35 PM
Post edited by Kenosis (User) on Apr 10, 2013, 8:47 AM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives