473,486 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Process each input record as an array of elements

Hello everyone,

I have a really simple question here:

I have a plain space delimited file 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 will be 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.
Jul 19 '05 #1
2 3725
This newsgroup is defunct. Use comp.lang.perl.misc instead.

This should do more or less what you want (season to taste):

while (<>) {
@elems = split; # split on whitespace
@elems[0,-1] = @elems[-1,0]; # swap first and last elements
print join(',', @elems), "\n";
}
Jul 19 '05 #2
Thanks Roy, it worked perfectly!

I did, however post the same question to per.msc group, just curious
about other possible alternatives.

Best regards,

Dmitry.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
728
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I...
1
3016
by: Dave | last post by:
Hi all, I was trying to make an image submit button with a rollover and discovered to my suprise that there is no way to access a form element of the "image" type. I tried specifying it by name...
10
1961
by: crjunk | last post by:
I have a script that I want to run only when my input box IS NOT disabled. Can someone tell me if something is wrong with my script? Is "disabled" the correct property to use? function...
2
1985
by: NotGiven | last post by:
I have a database result set with names, IDfield and a Y/N field. How do I create the radio buttons that can then be processed easily using an array (I assume) with an easy-to-loop-through...
9
2399
by: sherifffruitfly | last post by:
Hi, I've a got a little (exercise) program that reads data from a file and puts it into struct members. I run into trouble when one of the data pieces is comprised of several words (eg "john...
1
1102
by: D-Someone | last post by:
I am re-posting this message as originially it did not get a single response.. Any ideas? -------------- I am trying to come up with a good design for a web service that has some user logic...
6
2304
by: Mike | last post by:
I have a form that contains 240 "products". Each Product has a TR. Each TR contains a Yes and No radio button and a Product-Qty text input. A situation exists where I have to go through all the...
116
4510
by: dmoran21 | last post by:
Hi All, I am working on a program to take input from a txt file, do some calculations, and then output the results to another txt file. The program that I've written compiles fine for me, however,...
4
5780
by: Daniel | last post by:
is there some per-process-limit on memory in .net processes? is there any way to increase it? i keep getting System.OutOfMemoryException when my box has 8 gigs of unused memory.
0
7100
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7126
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4865
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.