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

print("@input") VS print(@input)

P: 14
Good Morning!

I am just starting to learn perl and I am somewhat mistifide about when I should do:

Expand|Select|Wrap|Line Numbers
  1. print("@input");
and when I should do:

Expand|Select|Wrap|Line Numbers
  1. print(@input)
Coming from Java I would do:

Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<array.length;i++){
  2.   System.out.print(array[i]);
  3. System.out.println();
  4.  
I cannot figure out why the print with the quotations adds leading spaces onto the output. See this example:

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my @input=<>;
  7.  
  8. for my $input (@input){
  9.     # $input=reverse($input);
  10. }
  11.  
  12. print("===\nIn Quotes:\n");
  13. print("@input");
  14.  
  15. print("\n\n===\nNOT In Quotes\n");
  16. print(@input);
  17.  
  18. print "End Program\n";
  19.  
Produces this:

Expand|Select|Wrap|Line Numbers
  1. jake-summers-computer:~/All Work/School Work/NLP/Ass2/Q3 jsummers$ cat SampleInput 
  2. Hello Everyone I am Jake.  
  3. This is the second line to the intro.  
  4.  
  5. Fourth line (third line is empty)jake-summers-computer:~/All Work/School Work/NLP/Ass2/Q3 jsummers$ 
  6. jake-summers-computer:~/All Work/School Work/NLP/Ass2/Q3 jsummers$ 
  7. jake-summers-computer:~/All Work/School Work/NLP/Ass2/Q3 jsummers$ 
  8. jake-summers-computer:~/All Work/School Work/NLP/Ass2/Q3 jsummers$ 
  9. jake-summers-computer:~/All Work/School Work/NLP/Ass2/Q3 jsummers$ ./line-reverseV2.pl < SampleInput 
  10. ===
  11. In Quotes:
  12. Hello Everyone I am Jake.  
  13.  This is the second line to the intro.  
  14.  
  15.  Fourth line (third line is empty)
  16.  
  17. ===
  18. NOT In Quotes
  19. Hello Everyone I am Jake.  
  20. This is the second line to the intro.  
  21.  
  22. Fourth line (third line is empty)End Program
  23. jake-summers-computer:~/All Work/School Work/NLP/Ass2/Q3 jsummers$ 
  24.  
Where is the space in front of " Fourth line (third line is empty)" coming from?
Oct 24 '09 #1
Share this Question
Share on Google+
2 Replies


Expert
P: 70
The short answer is... use the quotes when you want to print an additional
single space character between each element of your array.

I think a trivial example will help illustrate the difference between your
2 cases:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my @inputs = ('a', 'b', 'c');
  5.  
  6. print @inputs;
  7. print "\n";
  8.  
  9. print "@inputs";
  10. print "\n";
  11.  
  12. __END__
  13. abc
  14. a b c
  15.  
This behavior is described here:
http://perldoc.perl.org/perlvar.html

Search for LIST_SEPARATOR and OUTPUT_FIELD_SEPARATOR.
This will describe how to further control printed output, if needed.

The quotes are adding a space between all elements of you input
array. Since each element of your array has a trailing newline character,
all element of your array appear on separate lines of the output.
Apparently, your array has 4 elements (lines). The last 3 will have
a leading space.
Oct 26 '09 #2

P: 14
OOOOOOOOOOOOOOOOO. Ok that makes sense. Thanks for the reply.

Jake
Oct 26 '09 #3

Post your reply

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