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.
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.