a subroutine that creates a file with a given name and outputs the contents of a give

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jsos20
    New Member
    • Mar 2010
    • 11

    a subroutine that creates a file with a given name and outputs the contents of a give

    a subroutine that creates a file with a given name and outputs the contents of a array to given named file
    It should be called in the following way:

    &WriteToFile(`` suppliers.txt'' , @suppliers); where `@suppliers' contains all the ``name: number'' combinations for
    the suppliers, and ``suppliers.txt '' is the name of the file to create.
    need help on this one !!!
    Attached Files
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Are you and scuzz88 the same person, or just in the same class?

    We are not going to do your homework, but we can give you some guidance.

    Start by trying to write the sub yourself and when you get stuck, post your code and ask specific questions. Your post should include all warnings and error messages that your code produces.

    Your script should include these 2 lines near the top.
    Code:
    use strict;
    use warnings;
    BTW, your instructor is teaching you some very poor coding practices which tells me that (s)he doesn't know Perl very well.

    Comment

    • jsos20
      New Member
      • Mar 2010
      • 11

      #3
      of course i joined the class 1 month late
      i am sure its not the instructor but me!!!
      would be very great full for any help

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        What have you tried?

        Don't use '&' when calling the sub.

        Don't use prototypes i.e., instead of:
        Code:
        sub isnumber[B]()[/B] 
        {
        do this:
        Code:
        sub isnumber
        {
        Use proper indentation on the code blocks. Read 'perldoc perlstyle'


        Use the 3 arg form of open and use a lexical var for the filehandle when writing the data to 'suppliers.txt' .

        Comment

        • jsos20
          New Member
          • Mar 2010
          • 11

          #5
          #!/usr/bin/perl
          use strict;
          use warnings;
          #STEP1 - JOB:::::to create a file named suppliers.txt from names.txt and numbers.txt in the format ---------------> names:numbers these are in names.txt and numbers.txt respectively
          #ask the user for a name or number and search for the same, whether name or number.
          #STEP3 - user types a supplier name and it is append my supplier list to that file
          #STEP 4 - subroutine that creates a file with a given name and outputs the contents of a given array to that file

          my $arrayIndex;
          my $counter;
          my $numb;
          my $i=0;
          my $names=$ARGV[0];
          my $numbers=$ARGV[1];
          my $suppliers=$ARG V[2];

          my @names=(); # Arrays to store file content
          my @numbers=(); # Arrays to store file content
          my @suppliers=(); #array to store suppliers for later use in search

          sub WriteToFile
          {
          print "Please enter a file containing suppliers names: ";
          my $name=<STDIN>; #get the name of the phone numbers file
          chomp($name); #remove the newline character
          #open the names file for reading or, if problem, report an
          #error message

          if (-e "$name") #if the file exists try to open it
          {
          open(NUMBERS, "<", "$name") or die "Cannot open $name";
          #if there is any problem tell the user and exit
          }
          else #if the file does not exist tell the user and exit
          {
          die "$name does not exist.\n";
          }
          }

          open(NAMES, "<", "names") or die "Cannot open $names";

          print "Please enter a suppliers numbers file: ";
          my $number=<STDIN> ; #get the name of the phone numbers file
          chomp($number); #remove the newline character
          #open the numbers file for reading or, if problem, report an
          #error message

          if (-e "$number") #if the file exists try to open it
          {
          open(NUMBERS, "<", "$number") or die "Cannot open $number";
          #if there is any problem tell the user and exit
          }
          else #if the file does not exist tell the user and exit
          {
          die "$number does not exist.\n";
          }


          open(SUPPLIERS, ">", "suppliers.txt" ) or die "Cannot open $suppliers.txt $!\n"; # Open file for writing

          # Read and store file content of names and numbers
          while (<NAMES>)
          {
          push(@names,$_) ;
          }
          while (<NUMBERS>)
          {
          push(@numbers,$ _);
          }
          very new to perl but equally very excited to try it too
          please help as for indexing the suppliers array i give up!!!
          Last edited by jsos20; Mar 30 '10, 02:25 PM. Reason: appologies i am trying to indent but it does not show

          Comment

          • RonB
            Recognized Expert Contributor
            • Jun 2009
            • 589

            #6
            ALWAYS use the code tags when posing code.

            Please edit your post and put in the code tags.

            Comment

            • jsos20
              New Member
              • Mar 2010
              • 11

              #7
              i am indenting the post but it seams it is not uploading!!!

              Comment

              • RonB
                Recognized Expert Contributor
                • Jun 2009
                • 589

                #8
                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

                  #9
                  Code:
                  #!/usr/bin/perl
                  use strict;
                  use warnings;
                  #STEP1 - JOB:::::to create a file named suppliers.txt from names.txt and numbers.txt in the format ---------------> names:numbers these are in names.txt and numbers.txt respectively
                  #ask the user for a name or number and search for the same, whether name or number.
                  #STEP3 - user types a supplier name and it is append my supplier list to that file
                  #STEP 4 - subroutine that creates a file with a given name and outputs the contents of a given array to that file
                  
                  my $arrayIndex;
                  my $counter;
                  my $numb;
                  my $i=0;
                  my $names=$ARGV[0];
                  my $numbers=$ARGV[1];
                  my $suppliers=$ARGV[2];
                  
                  my @names=(); # Arrays to store file content
                  my @numbers=(); # Arrays to store file content
                  my @suppliers=(); #array to store suppliers for later use in search
                  
                  sub WriteToFile
                  {
                  print "Please enter a file containing suppliers names: ";
                  my $name=<STDIN>; #get the name of the phone numbers file
                  chomp($name); #remove the newline character
                  #open the names file for reading or, if problem, report an
                  #error message
                  
                  if (-e "$name") #if the file exists try to open it
                  {
                  open(NUMBERS, "<", "$name") or die "Cannot open $name";
                  #if there is any problem tell the user and exit
                  }
                  else #if the file does not exist tell the user and exit
                  {
                  die "$name does not exist.\n";
                  }
                  }
                  
                  open(NAMES, "<", "names") or die "Cannot open $names";
                  
                  print "Please enter a suppliers numbers file: ";
                  my $number=<STDIN>; #get the name of the phone numbers file
                  chomp($number); #remove the newline character
                  #open the numbers file for reading or, if problem, report an
                  #error message
                  
                  if (-e "$number") #if the file exists try to open it
                  {
                  open(NUMBERS, "<", "$number") or die "Cannot open $number";
                  #if there is any problem tell the user and exit
                  }
                  else #if the file does not exist tell the user and exit
                  {
                  die "$number does not exist.\n";
                  }
                  
                  
                  open(SUPPLIERS, ">", "suppliers.txt") or die "Cannot open 
                  $suppliers.txt $!\n"; # Open file for writing
                  
                  # Read and store file content of names and numbers
                  while (<NAMES>)
                  {
                  push(@names,$_);
                  }
                  while (<NUMBERS>)
                  {
                  push(@numbers,$_);
                  }
                  very new to perl but equally very excited to try it too
                  please help as for indexing the suppliers array i give up!!!

                  Comment

                  • jsos20
                    New Member
                    • Mar 2010
                    • 11

                    #10
                    not to bother you
                    but could you please tell
                    me the process of writing a subroutine, calling it,
                    basically i want to know all that subroutines involve
                    i have been reading "picking up perl" but still cant get subroutines

                    Comment

                    • RonB
                      Recognized Expert Contributor
                      • Jun 2009
                      • 589

                      #11
                      See: 'perldoc perlsub'

                      Comment

                      • jsos20
                        New Member
                        • Mar 2010
                        • 11

                        #12
                        have been trying to read it but its difficult

                        so i will just send what i have in
                        thanks for everything
                        jsos

                        Comment

                        Working...