473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

combining 2 lists in perl

1
I am new to programing and am trying to learn. I am trying to use perl to merge to list of data. for example:
list 1:
aaaa
bbbb
cccc
dddd
list 2:
aaaa
bbbb
cccc
dddd
The output that I want is:
aaaa bbbb
bbbb ccccc
cccc dddd
I have written a perl script and can get this type of an output:
aaaa aaaa
bbbb aaaa
cccc aaaaa
dddd aaaa

This is my script:

[/open FILE1, "<myfile.txt" or die $!;
while (<FILE1>)
{
my $file1_line = $_;
chomp $file1_line;

open FILE2, "<myfile.txt" or die $!;
while (<FILE2>)
{
my $file2_line = $_;
chomp $file2_line;

printf ("%s\t%s\n", $file1_line, $file2_line);

}
close (FILE2);
}
close (FILE1); ]


I am not sure what I am doing wrong. any help would be greatly appreciated.

thanks
Nov 2 '11 #1
3 2580
miller
1,089 Expert 1GB
Why do you want this output?

Expand|Select|Wrap|Line Numbers
  1. aaaa bbbb
  2. bbbb ccccc
  3. cccc dddd
  4.  
What meaningful algorithm is this demonstrating, if any?

- Miller
Nov 2 '11 #2
chorny
80 Expert
You are reading whole second file for every line of first.
Nov 11 '11 #3
Hi,
please use this program.


open FILE1, "<myfile.txt" or die $!;
my $count=2;
while (<FILE1>)
{
my $file1_line = $_;
chomp $file1_line;
open FILE2, "<myfile.txt" or die $!;
my $temp_count=1;
while (<FILE2>)
{
my $file2_line = $_;
chomp $file2_line;
if($count eq $temp_count)
{
printf ("%s\t%s\n", $file1_line, $file2_line);
$count++;
last;
}
$temp_count++;
}
close (FILE2);
}
close (FILE1);
Dec 29 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Xah Lee | last post by:
another functional exercise with lists. Here's the perl documentation. I'll post a perl and the translated python version in 48 hours. =pod parti(aList, equalFunc) given a list aList of...
0
by: Mark Healey | last post by:
As part of my own learning mysql project I'm planning to build databases for all my books and DVD's. Stephen Hawking is probably a better typist than I am so I plan to use barcodes to get the info...
102
by: Xah Lee | last post by:
i had the pleasure to read the PHP's manual today. http://www.php.net/manual/en/ although Pretty Home Page is another criminal hack of the unix lineage, but if we are here to judge the quality...
28
by: hlubenow | last post by:
Hello, I really like Perl and Python for their flexible lists like @a (Perl) and a (Python), where you can easily store lots of strings or even a whole text-file. Now I'm not a...
3
by: wombat | last post by:
I have a question about using arrays in Perl, involving connecting to MySQL databases. In the past I've used PHP to access databases and I've been playing around now with Perl. In PHP to access...
5
by: TokiDoki | last post by:
Hi! I have a Python problem which is my last problem to solve to finish up a Django application. This is amazingly simple but I have been stuck now for a couple of days. It is embarrisingly...
2
by: peteinglastonbury | last post by:
I'd be most grateful if someone would help me. I hope I'm in the right forum (apologies if not) - I'm not sure whether my problem is CGI or Javascript related. I found a script called...
6
by: weg22 | last post by:
Hi all, I have two lists of textboxes: 'Create list of all textboxes on error log data tab el_textboxes = New List(Of Windows.Forms.TextBox)(New Windows.Forms.TextBox() {addr_x100, addr_x101,...
1
by: colin | last post by:
Hi, I have 3 object of interest wich are objects in 3d space, surface,wire,point. they are all interconnected, and they all contain lists of objects they are connected to, eg each surface will...
6
by: Jeremy Goodman | last post by:
Access 2007; Merging records containing multivalue drop down lists. I have a database showing legislation information divided by State/territory. The database needs to be able to show the info...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...

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.