473,480 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

14 New Member
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
2 2438
toolic
70 Recognized Expert New Member
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
sixtyfootersdude
14 New Member
OOOOOOOOOOOOOOOOO. Ok that makes sense. Thanks for the reply.

Jake
Oct 26 '09 #3

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

Similar topics

4
9344
by: arotem | last post by:
Hi, I am trying to call an unbound method (PrintInput) with the object instance as the first argument but getting the following error: "TypeError: unbound method PrintInput() must be called with...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7037
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4770
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4476
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.