put all lines of one file with all lines of another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scuzz88
    New Member
    • Mar 2010
    • 4

    put all lines of one file with all lines of another

    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?
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

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

    Regards,

    Jeff

    Comment

    • scuzz88
      New Member
      • Mar 2010
      • 4

      #3
      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
      Code:
      #!/usr/bin/perl
      
      use strict;
      
      use warnings;
      
      my $outputfile="suppliers.txt";
      
      print "\nIan O'Brien (R00059275)";
      
      print "\nPlease enter filename containing suppliers names: ";
      
      my $inputfile=<STDIN>;
      chomp($inputfile);
      
      open(INFILE, "<", "$inputfile") or die "Cannot open $inputfile.";
      
      open(OUTFILE, ">>", "$outputfile")	or die "cannot open $outputfile.";
      
      my @names = "$inputfile";
      while (<INFILE>) {
      	push (@names, $_);
      }
      
      print "\nPlease enter filename containing suppliers numbers: ";
      
      my $inputfile2=<STDIN>;
      chomp($inputfile2);
      
      open(FILE, "<", "$inputfile2") or die "Cannot open $inputfile2.";
      
      my @numbers = "$inputfile2";
      
      my $acode;
      my $num;
      
      while ($acode = <FILE>)				# $num is = to what is on the first line of the INFILE 
      {
      	$num = <FILE>;				# $num is = to what is on the second line of INFILE
      
      	chomp($acode2,$num);
      
      print $acode . $num;
      
      
      close INFILE;
      
      close FILE;
      
      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:
      &WriteToFile(“s uppliers.txt”,@ suppliers);
      Where ‘@supplier s’ 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:
      Code:
      my $index=&find($item, @array);
      if ($index>=0)
      {#means the item was found at position $index}
      Else
      {#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.
      Code:
      Sub isnumber ()
      {
      	my $item = shift; #get the search item to be checked from the argument list
      	if  ($item =~ /\d/)	#if the item has a digit in it
      	{
      		return 1;	#i.e return true
      	}
      	else
      	{
      		return 0;	#i.e return false
      	}
      }
      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.
      Last edited by numberwhun; Mar 29 '10, 05:36 PM. Reason: Please use CODE TAGS!!!

      Comment

      • jsos20
        New Member
        • Mar 2010
        • 11

        #4
        sorry was trying to post to other guy

        Comment

        • RonB
          Recognized Expert Contributor
          • Jun 2009
          • 589

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

          Comment

          • jsos20
            New Member
            • Mar 2010
            • 11

            #6
            sound out
            thanks
            jsos

            Comment

            Working...