By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,178 Members | 971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,178 IT Pros & Developers. It's quick & easy.

put all lines of one file with all lines of another

P: 4
i have two files names.txt and numbers.txt which contain names and phone numbers of suppliers, line 1 of names.txt corrosponds with the line 1 of the numbers.txt file, what i wish to do is to execute the names and numbers files into another file called suppliers.txt like so name: number,

could anyone help?
Mar 28 '10 #1
Share this Question
Share on Google+
5 Replies

Expert Mod 2.5K+
P: 3,503
What have you tried so far? Show us your code and we will help you where you get stuck. This is a great learning example. Also, please provide a sample of your data to go with your code, so that we may see what you are working with.


Mar 28 '10 #2

P: 4
Im a newbie to perl and was given this to do for six o clock tonight and have not had time as a member of my family passed away on friday.

This is what i have so far an i know a lot of it is wrong and really need help
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  3. use strict;
  5. use warnings;
  7. my $outputfile="suppliers.txt";
  9. print "\nIan O'Brien (R00059275)";
  11. print "\nPlease enter filename containing suppliers names: ";
  13. my $inputfile=<STDIN>;
  14. chomp($inputfile);
  16. open(INFILE, "<", "$inputfile") or die "Cannot open $inputfile.";
  18. open(OUTFILE, ">>", "$outputfile")    or die "cannot open $outputfile.";
  20. my @names = "$inputfile";
  21. while (<INFILE>) {
  22.     push (@names, $_);
  23. }
  25. print "\nPlease enter filename containing suppliers numbers: ";
  27. my $inputfile2=<STDIN>;
  28. chomp($inputfile2);
  30. open(FILE, "<", "$inputfile2") or die "Cannot open $inputfile2.";
  32. my @numbers = "$inputfile2";
  34. my $acode;
  35. my $num;
  37. while ($acode = <FILE>)                # $num is = to what is on the first line of the INFILE 
  38. {
  39.     $num = <FILE>;                # $num is = to what is on the second line of INFILE
  41.     chomp($acode2,$num);
  43. print $acode . $num;
  46. close INFILE;
  48. close FILE;
  50. close OUTFILE;
This is what i was given to do below

The programe should ask the user for the names of these files, open these files, and read the names and numbers into two arrays. Note: When reading in from the file, read into a temporary variable and then place its value into the next place in the destination array.
The programme should complete two tasks:
1. create a new file called suppliers.txt containing the names and numbers together in the following format with one supplier per line:
name: number
2. ask the user for a name or number to search for. Regardless of whether they give a name or number, the programme should output the following name and number to the screen. If the name or number cannot be found it should report that.

Your program should have the following subroutines:
1. a subroutine that creates a file with a given name and outputs the contents of a given array to that file. This can be used for task 1 above.
It should be called in the following way:
Where ‘@suppliers’ contains all the “name: number” combinations for the suppliers, and “suppliers.txt” is the name of the file to create.
2. a subroutinethat searches an array for an item (see below for a description of the steps required to search). If it finds the item it should return the array index of the item, if it does not find then the return value should be -1. This can be used for task 2 above. It should be called in the following way:
Expand|Select|Wrap|Line Numbers
  1. my $index=&find($item, @array);
  2. if ($index>=0)
  3. {#means the item was found at position $index}
  4. Else
  5. {#means the item was not found}
Note: when checking that the item to be found is not equal to the array element being examined, use the ‘does not match’ operaor ‘!~’ which does not care wheather the item is a number or a piece of text. For example: to check that $item does not match the $ith element of the @array: if ($item !~ /$a[$i]/)
You will need another subroutine as given below to determine if some input is a name or number. This can be called on in an ‘if’ condition to determine if some variable is a number. Your program can assume that whatever is not a number is a name.
Expand|Select|Wrap|Line Numbers
  1. Sub isnumber ()
  2. {
  3.     my $item = shift; #get the search item to be checked from the argument list
  4.     if  ($item =~ /\d/)    #if the item has a digit in it
  5.     {
  6.         return 1;    #i.e return true
  7.     }
  8.     else
  9.     {
  10.         return 0;    #i.e return false
  11.     }
  12. }
Search Algorithm
The following steps describe how a program may search an array for an item:
1. search an index at the first position of the array
2. continue moving the index up the array while:
a. the array index is still valid AND
b. the item in the array does not match the item sought (use!~ instead or != or ne)
3. at the end of the loop in step 2, if the array index is valid then the item must have been found so return the array index. Otherwise the loop must have stopped because it went past the end of the array so return -1.
Mar 29 '10 #3

P: 11
sorry was trying to post to other guy
Mar 30 '10 #4

Expert Mod 100+
P: 589
When posting, click in the 'Go Advanced' button.

The # button at the far right of the menu bar is the code tag.

Select your code and press the code tag button.

Preview the changes and submit when you're finished.
Mar 30 '10 #5

P: 11
sound out
Mar 30 '10 #6

Post your reply

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