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

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

jsos20
P: 11
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
File Type: txt bla dont delete.txt (2.6 KB, 309 views)
Mar 30 '10 #1

✓ answered by jsos20

have been trying to read it but its difficult

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

Share this Question
Share on Google+
11 Replies


Expert Mod 100+
P: 589
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.
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. 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.
Mar 30 '10 #2

jsos20
P: 11
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
Mar 30 '10 #3

Expert Mod 100+
P: 589
What have you tried?

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

Don't use prototypes i.e., instead of:
Expand|Select|Wrap|Line Numbers
  1. sub isnumber() 
do this:
Expand|Select|Wrap|Line Numbers
  1. sub isnumber
Use proper indentation on the code blocks. Read 'perldoc perlstyle'
http://perldoc.perl.org/perlstyle.html

Use the 3 arg form of open and use a lexical var for the filehandle when writing the data to 'suppliers.txt'.
http://perldoc.perl.org/functions/open.html
Mar 30 '10 #4

jsos20
P: 11
#!/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!!!
Mar 30 '10 #5

Expert Mod 100+
P: 589
ALWAYS use the code tags when posing code.

Please edit your post and put in the code tags.
Mar 30 '10 #6

jsos20
P: 11
i am indenting the post but it seams it is not uploading!!!
Mar 30 '10 #7

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

jsos20
P: 11
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. #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
  5. #ask the user for a name or number and search for the same, whether name or number.
  6. #STEP3 - user types a supplier name and it is append my supplier list to that file
  7. #STEP 4 - subroutine that creates a file with a given name and outputs the contents of a given array to that file
  8.  
  9. my $arrayIndex;
  10. my $counter;
  11. my $numb;
  12. my $i=0;
  13. my $names=$ARGV[0];
  14. my $numbers=$ARGV[1];
  15. my $suppliers=$ARGV[2];
  16.  
  17. my @names=(); # Arrays to store file content
  18. my @numbers=(); # Arrays to store file content
  19. my @suppliers=(); #array to store suppliers for later use in search
  20.  
  21. sub WriteToFile
  22. {
  23. print "Please enter a file containing suppliers names: ";
  24. my $name=<STDIN>; #get the name of the phone numbers file
  25. chomp($name); #remove the newline character
  26. #open the names file for reading or, if problem, report an
  27. #error message
  28.  
  29. if (-e "$name") #if the file exists try to open it
  30. {
  31. open(NUMBERS, "<", "$name") or die "Cannot open $name";
  32. #if there is any problem tell the user and exit
  33. }
  34. else #if the file does not exist tell the user and exit
  35. {
  36. die "$name does not exist.\n";
  37. }
  38. }
  39.  
  40. open(NAMES, "<", "names") or die "Cannot open $names";
  41.  
  42. print "Please enter a suppliers numbers file: ";
  43. my $number=<STDIN>; #get the name of the phone numbers file
  44. chomp($number); #remove the newline character
  45. #open the numbers file for reading or, if problem, report an
  46. #error message
  47.  
  48. if (-e "$number") #if the file exists try to open it
  49. {
  50. open(NUMBERS, "<", "$number") or die "Cannot open $number";
  51. #if there is any problem tell the user and exit
  52. }
  53. else #if the file does not exist tell the user and exit
  54. {
  55. die "$number does not exist.\n";
  56. }
  57.  
  58.  
  59. open(SUPPLIERS, ">", "suppliers.txt") or die "Cannot open 
  60. $suppliers.txt $!\n"; # Open file for writing
  61.  
  62. # Read and store file content of names and numbers
  63. while (<NAMES>)
  64. {
  65. push(@names,$_);
  66. }
  67. while (<NUMBERS>)
  68. {
  69. push(@numbers,$_);
  70. }
  71. very new to perl but equally very excited to try it too
  72. please help as for indexing the suppliers array i give up!!!
  73.  
Mar 30 '10 #9

jsos20
P: 11
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
Mar 30 '10 #10

Expert Mod 100+
P: 589
See: 'perldoc perlsub'
http://perldoc.perl.org/perlsub.html
Mar 30 '10 #11

jsos20
P: 11
have been trying to read it but its difficult

so i will just send what i have in
thanks for everything
jsos
Mar 30 '10 #12

Post your reply

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