Process each input record as an array of elements

D

Dmitry

Hello everyone,

I have a really simple question here:

I have a plain file with space delimited fields that I want to read with WHILE
loop 1 line at the time and process each input record as an array of
elements. Then, on the next iteration of the loop, clean up the array
and populate it with contents of the next input record. Each input
record consists of 7 fields / elements separated by spaces. The reason
I need this is because I have to switch FIRST and LAST elements of the
record by places. For example:

Before: aaa bbb ccc ddd eee fff ggg

After: ggg bbb ccc ddd eee fff aaa

I am having trouble assigning contents of each input record to an
array.

If I can do that, then I can use POP and UNSHIFT Perl functions to
switch the elements.

Here is what I have so far:

my $pop_element = '';
my @print_array = ();

open (OLD, "< $fname_out") || die "Cannot open OLD file
$fname_out!";
open (NEW, "> $temp") || die "Cannot open NEW file $temp!";

while (<OLD>) {
@print_array = "$_\n";
$pop_element = pop ( @print_array );
unshift ( @print_array, $pop_element );
$_ = @print_array;
print NEW "$_\n" || die "Cannot write NEW file $temp!";
}
close (OLD) || die "Cannot close OLD file $fname_out!";
close (NEW) || die "Cannot close NEW file $temp!";
rename ( $temp, $fname_out ) || die "Cannot rename NEW file $temp
to $fname_out!";
}

Any suggestions would be greatly appreciated.

Thanks,

Dmitry.
 
L

LaDainian Tomlinson

Dmitry said:
I have a plain file with space delimited fields that I want to read with WHILE
loop 1 line at the time and process each input record as an array of
elements. Then, on the next iteration of the loop, clean up the array
and populate it with contents of the next input record. Each input
record consists of 7 fields / elements separated by spaces. The reason
I need this is because I have to switch FIRST and LAST elements of the
record by places. For example:

Before: aaa bbb ccc ddd eee fff ggg

After: ggg bbb ccc ddd eee fff aaa

I am having trouble assigning contents of each input record to an
array.

<snip>

You may not need to use an array as such. You could just do a substitution:

#!/usr/bin/perl
$\ = "\n";
while (<DATA>){
chomp;
s/^(\S+)\s+(.*?)\s+(\S+)$/$3 $2 $1/;
print;
}

__DATA__
aaa bbb ccc ddd eee fff ggg
hhh ii jjjj kk lllll m n


Hope that helps,

Brandan L.
 
R

Roy Johnson

If I can do that, then I can use POP and UNSHIFT Perl functions to
switch the elements.

You can also just swap them by indexing:

@print_array[0,-1] = @print_array[-1,0];
 
D

Dmitry

Thanks everyone for your input!

I have implemented Roy's recommendation and it seems to be the
simplest and proper one to use.

Thanks again to all.

Regards,

Dmitry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top