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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need Help in Report File Handling

 



mar85
Novice

Apr 9, 2013, 10:22 PM

Post #1 of 5 (407 views)
Need Help in Report File Handling Can't Post

Hi,

I wanted to write a perl script to open a list.txt, grab particular data in it, and then arrange it, then save it to a new file list_arranged.txt.

The list.txt looks like this (a simple example):


Quote
Group #1:
(F) FRUITS orange[0]/apples[1]/mango[3]
(F) FRUITS grape[0]/cherry[2]/papaya[3]

Group #2:
(B) FRUITS orange.0/apples.1/mango.3
(B) FRUITS grape.0/cherry.2/papaya.3

Groups Combined:
(F) FRUITS orange[0]/apples[1]/mango[3]
(F) FRUITS grape[0]/cherry[2]/papaya[3]
(B) FRUITS orange.0/apples.1/mango.3
(B) FRUITS grape.0/cherry.2/papaya.3


How can I arrange it so that it becomes:


Quote
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


Thanks


mar85
Novice

Apr 9, 2013, 10:30 PM

Post #2 of 5 (406 views)
Re: [mar85] Need Help in Report File Handling [In reply to] Can't Post

This is the body of my script right now:


Code
use Getopt::Long; 

GetOptions(
"txt_path:s" => \$txt,
"output_txt_path:s" => \$out_txt
);

chomp($txt);
chomp($out_txt);
$out_txt =~ s/^\s+//; #remove leading spaces
$out_txt =~ s/\s+$//; #remove trailing spaces

open(IN_TEXT,$txt);
my @txt_contents = <IN_TEXT>;
close(IN_TEXT);

foreach (@txt_contents) {
if ( $_ =~ m/FRUITS/g) {
my $filtered_content = custom_subroutine($_);
push(@Sorted_Content,$filtered_content);
}
}

open(IN_OUTPUT_FILE,">$out_txt.txt");
foreach (@Sorted_Content)
{
print IN_OUTPUT_FILE;
}



(This post was edited by mar85 on Apr 9, 2013, 10:32 PM)


Kenosis
User

Apr 9, 2013, 11:16 PM

Post #3 of 5 (397 views)
Re: [mar85] Need Help in Report File Handling [In reply to] Can't Post

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


Code
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 script.pl list.txt >list_arranged.txt

Output:


Code
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)


mar85
Novice

Apr 15, 2013, 11:11 PM

Post #4 of 5 (368 views)
Re: [Kenosis] Need Help in Report File Handling [In reply to] Can't Post

Thanks for the reply Kenosis


Kenosis
User

Apr 16, 2013, 9:58 AM

Post #5 of 5 (354 views)
Re: [mar85] Need Help in Report File Handling [In reply to] Can't Post

You're most welcome, mar85!

 
 


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

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