LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl Array delimiter (https://www.linuxquestions.org/questions/programming-9/perl-array-delimiter-777384/)

ashok.g 12-22-2009 01:09 AM

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?

Disillusionist 12-22-2009 02:52 AM

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;


ashok.g 12-22-2009 03:10 AM

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.

radoulov 12-22-2009 04:41 AM

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), $/;
  '


ghostdog74 12-22-2009 05:07 AM

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.

ashok.g 12-22-2009 05:16 AM

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.

ghostdog74 12-22-2009 05:58 AM

Quote:

Originally Posted by ashok.g (Post 3801050)
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/

Telemachos 12-22-2009 08:11 AM

Quote:

Originally Posted by Disillusionist (Post 3800968)
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

ashok.g 12-23-2009 05:50 AM

Quote:

Originally Posted by ghostdog74 (Post 3801072)
do you understand what he is doing?

Ya,I do.


All times are GMT -5. The time now is 05:42 PM.