LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-22-2009, 01:09 AM   #1
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
Perl Array delimiter


Hello Friends,
I am learning Perl in Linux these days. I have the following code which prints the array using delimiter as "\n"(new line).
Code:
#!/usr/bin/perl
@me=( I,me,myself );
foreach (@me)
{
print $_,"\n";
}
I know that we can print the array contents as;
Code:
print @me;
Now, my requirement is can we use such a simple command like print @me; to print array contents using a delimiter.
What is that?
 
Old 12-22-2009, 02:52 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
You are not using "\n" as a delimiter.

You are processing each element of an array and printing "\n" after the element to throw a new line.

You would need to modify each element to include the newline, which is far more complex than what you are currently doing:
Code:
#!/usr/bin/perl

@me=(I,me,myself);
$scal=(@me);
for ( $count=0; $count<$scal; $count++)
{
   $temp=$me[$count];
   $me[$count]="$temp\n";
}
print @me;
 
Old 12-22-2009, 03:10 AM   #3
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
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.
 
Old 12-22-2009, 04:41 AM   #4
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Code:
perl -E'
  @me=( I, me, myself );
  $, = $/; 
  say @me;
  '
For Perl < 5.10:
Code:
perl -le'
  @me=( I, me, myself );
  $, = $/; 
  print @me;
  '
Or:

Code:
perl -le'
  @me=( I, me, myself );
  print join $/, @me;
  '
Code:
perl -e'
  @me=( I, me, myself );
  print +(join $/, @me), $/;
  '
 
1 members found this post helpful.
Old 12-22-2009, 05:07 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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.
 
Old 12-22-2009, 05:16 AM   #6
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Thank you radoulov,
This is what exactly I want.


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.
 
Old 12-22-2009, 05:58 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by ashok.g View Post
Thank you radoulov,
This is what exactly I want.
do you understand what he is doing?

Quote:
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/
 
Old 12-22-2009, 08:11 AM   #8
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Quote:
Originally Posted by Disillusionist View Post
You are not using "\n" as a delimiter.

You are processing each element of an array and printing "\n" after the element to throw a new line.

You would need to modify each element to include the newline, which is far more complex than what you are currently doing:
Code:
#!/usr/bin/perl

@me=(I,me,myself);
$scal=(@me);
for ( $count=0; $count<$scal; $count++)
{
   $temp=$me[$count];
   $me[$count]="$temp\n";
}
print @me;
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.
  1. 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
    }
  2. 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;
  3. The map code also shows that you don't need temporary variables (usually) in Perl. The interpreter takes care of that for you.

@Ashok.g, I highly recommend this article for reviewing Perl's special variables. It specifically discusses what you're trying to do: http://www.perl.com/pub/a/2004/06/18/variables.html

Last edited by Telemachos; 12-22-2009 at 08:46 AM. Reason: Add link
 
1 members found this post helpful.
Old 12-23-2009, 05:50 AM   #9
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by ghostdog74 View Post
do you understand what he is doing?
Ya,I do.
 
  


Reply

Tags
array, perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How use CUT -d 'delimiter' is delimiter is a TAB? frenchn00b Programming 12 11-06-2013 03:17 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
perl + @INC array Ateo Programming 0 01-22-2007 01:38 PM
Issues calling sort from perl using pipe as delimiter amytys Programming 1 10-20-2004 09:35 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM


All times are GMT -5. The time now is 06:03 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration