in reply to Re: Arranging multiple lines
in thread Arranging multiple lines

Thank you very much hdb,

but again you have to forgive my ignorance on this, I got lost in incorporating your code with input and output file arguments. :(

Please bear with me, I just started studying perl 2 days ago.

use strict; use warnings; my $infile = $ARGV[0]; my $outfile = $ARGV[1]; open my $in, "<", $infile or die $!; open my $out, ">", $outfile or die $!; sub printdata { my $data = shift; $$data{"User-Agent"} //= "--"; print join "|", @$data{ ( "Arrival Time", "From", "To", "User- +Agent" ) }; print "\n\n"; %$data = (); } my %data; my ($item, $value); print "Arrival Time|From|To|User-Agent\n\n"; while(<$in>){ chomp; next unless ($item, $value) = /^(.*?): (.*)/; printdata \%data if $item eq "Arrival Time" and %data; $data{$item} = $value; } printdata \%data; close $in; close $out;

Replies are listed 'Best First'.
Re^3: Arranging multiple lines
by hdb (Monsignor) on Jun 03, 2013 at 13:13 UTC

    One way to do it is to pass the $out handle into the printdata function. Beware of commata after $out in this example:

    use strict; use warnings; sub printdata { my $out = shift; my $data = shift; $$data{"User-Agent"} //= "--"; print $out join "|", @$data{ ( "Arrival Time", "From", "To", "User +-Agent" ) }; print $out "\n\n"; %$data = (); } open my $out, ">", "tmp.txt"; my %data; my ($item, $value); print $out "Arrival Time|From|To|User-Agent\n\n"; while(<DATA>){ chomp; next unless ($item, $value) = /^(.*?): (.*)/; printdata $out, \%data if $item eq "Arrival Time" and %data; $data{$item} = $value; } printdata $out, \%data;

    Alternatively, you could assemble a string in printdata, return it from the sub and print later.