473,763 Members | 3,901 Online
Bytes | Software Development & Data Engineering Community
+ 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 3737
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 can pass a max of 512 words to it at a time. So I would pass them in chunks of 512 words until the whole file has been processed. I haven't worked with binary files before so I'm confused with how to store the binary file into memory. What sort of...
1
3051
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 and also by the elements array with no luck. Even weirder, I can sandwich an image button in between other input types and then write a script diplaying the contents of the elements array and it acts as if the image button does not exist at all!...
10
1989
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 TextChanged(i){ if (!document.ScheduleForm.disabled) { document.ScheduleForm.txtRecordStatus.value = "Changes Made; Record Not Saved."; document.ScheduleForm.txtRecordStatus.style.color = "#FF0000"; }
2
2001
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 structure? The form would look like: Joe Smith O O Karen Smith O O ....
9
2429
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 doe", with a space in it). For console input, cin.getline(var, howMuchIWant) or cin.get() has done the trick for me in the past. It doesn't seem to work for me nearly so well with a file stream. I wouldn't have thought cpp regarded
1
1113
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 and business processes involved. Here a small sample of the process flow: --- Client Get's User Input for field 1...
6
2319
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 products and check the No radio button for all the products. In javascript, that is easy enough. When the No radio button is clicked, I have to put '0' in the Product-Qty text input, and disable the Product-Qty text input. I create an array of...
116
4618
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, when I run it, it stalls and does nothing. I'm wondering if there's something obvious that I'm missing. My code is below and any help would be appreciated. Thanks, Dave
4
5806
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
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9998
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
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 we have to send another system
2
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.