ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Disillusionist,
I think we both did same thing(you appended \n for each element and then retrieved the array, I printed each element of array followed by \n).Indeed, you are changing my array entirely.
My concern is all about to print the array with some delimiter without actually changing my array.
my suggestion, before you digest and use the solutions here, is to re-read the Perl documentation and get your basics right.
perldoc perl, perldoc perltoc or perldoc perlintro is what you need right now.
ghostdog74,
How can I get "perldoc perl, perldoc perltoc or perldoc perlintro"? Did you mean to download them from the links you put in your signature?Anyway, thanks for your suggestion.
ghostdog74,
How can I get "perldoc perl, perldoc perltoc or perldoc perlintro"? Did you mean to download them from the links you put in your signature?Anyway, thanks for your suggestion.
type it on your command prompt. otherwise, go to the site with your browser: http://perldoc.perl.org/
Just for the record, this is completely wrong. He does want to print the array delimited by newlines. There are plenty of ways to do what the OP wants (as Radoulov showed), but beyond that, your suggested fix is not idiomatic Perl.
There's no reason to get the length of the array and iterate over it using a three part for loop. A foreach loop in Perl automatically knows the length of the array and iterates accordingly. You can also write foreach in Perl as for. The interpreter figures out which it is by context. So to iterate over an array, you can simply do this:
Code:
for my $item (@me) {
# do stuff to $item
}
I think that you may be doing it this way because you are tampering with the array items as you iterate over the array. In general, I think that map is a much easier to write method for this.
Code:
@me = map { $_ . "\n" } @me;
The map code also shows that you don't need temporary variables (usually) in Perl. The interpreter takes care of that for you.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.