For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2007 > Array Manipulation









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Array Manipulation
Irfan Sayed

2007-10-25, 7:59 am

Hi All,

I have one array say my @test=(1,2,3,4,5);
if I print this array it will print like this
print "@test\n";
and the output is
1 2 3 4 5

now my req. is that I want to store these array values in another array
in such a fashion where I can print like
1
2
3
4
5

so I mean to say that if I type print "@test1\n";
then output should come as
1
2
3
4
5

I have used push function also but it is not giving expected result.

Please guide.

Regards
Irfan.



Dyana Wu

2007-10-25, 7:59 am


On 25 Oct 2007, at 4:59 PM, Sayed, Irfan (Irfan) wrote:

> Hi All,
>
> I have one array say my @test=(1,2,3,4,5);
> if I print this array it will print like this
> print "@test\n";
> and the output is
> 1 2 3 4 5
>
> so I mean to say that if I type print "@test1\n";
> then output should come as
> 1
> 2
> 3
> 4
> 5


Try map:

my @test1 = map($_."\n", @test);
print @test1, "\n";

Note that if you use print "@test1\n", Perl will insert a space after
each element, this means that you will actually get:

1
2
3
4
5

Alternatively, if you simply need to print each element of @test with
a newline after it:

print $_, "\n" foreach @test;
print "\n";

HTH.

--
dwu
Gunnar Hjalmarsson

2007-10-25, 7:59 am

Sayed, Irfan (Irfan) wrote:
> I have one array say my @test=(1,2,3,4,5);
> if I print this array it will print like this
> print "@test\n";
> and the output is
> 1 2 3 4 5
>
> now my req. is that I want to store these array values in another array
> in such a fashion where I can print like
> 1
> 2
> 3
> 4
> 5
>
> so I mean to say that if I type print "@test1\n";
> then output should come as
> 1
> 2
> 3
> 4
> 5


{
local $" = "\n";
print "@test\n";
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Ron Bergin

2007-10-25, 7:02 pm

On Oct 25, 1:59 am, isa...@avaya.com (Irfan Sayed) wrote:
> Hi All,
>
> I have one array say my @test=(1,2,3,4,5);
> if I print this array it will print like this
> print "@test\n";
> and the output is
> 1 2 3 4 5
>
> now my req. is that I want to store these array values in another array
> in such a fashion where I can print like
> 1
> 2
> 3
> 4
> 5
>
> so I mean to say that if I type print "@test1\n";
> then output should come as
> 1
> 2
> 3
> 4
> 5
>
> I have used push function also but it is not giving expected result.
>
> Please guide.
>
> Regards
> Irfan.


print $_,$/ for @test;

Nobull67@Gmail.Com

2007-10-25, 7:02 pm

On Oct 25, 4:21 pm, r...@i.frys.com (Ron Bergin) wrote:
>
> print $_,$/ for @test;


Nothing wrong with that, but I usually write:

print "$_\n" for @test;

TMTOWTDI!

asmith9983@gmail.com

2007-10-25, 7:02 pm

Hi
This will do what you want:-

perl -le '@test=(1,2,3,4,5);print join "\n",@test;'

The -l option ensures a final newline after the last element of the array is
printed. The order of the options is important as changing it to "el"
wouldn't
work.

--
Andrew
Edinburgh,Scotland

On Thu, 25 Oct 2007, Ron Bergin wrote:

> On Oct 25, 1:59 am, isa...@avaya.com (Irfan Sayed) wrote:
>
> print $_,$/ for @test;
>
>
>

Greg

2007-10-25, 7:02 pm

Similar issue here, but with a twist.
I have an input file that I'm reading in that is pipe delimited. (HL7
actually)
So far I have
my @record = split (/\|/,$_);
I want to take $record[16] and replace it with $record[16] /
$record[7] ONLY if $record[7] is not empty.
I have this accomplished by
$converted = $record[16];
$converted = ($record[16] / $record[7]) unless ($record[7] eq "");
$record[16]="$converted";

At this point I print out @record and it gives correct values..
without the |'s.
To get it back into the line of the input file in memory can I just do
a
print $_, "|" foreach @record; ?
or how would I do that exactly?

John W . Krahn

2007-10-28, 4:00 am

On Thursday 25 October 2007 11:03, asmith9983@gmail.com wrote:
> Hi


Hello, Please do not top-post, TIA.

> This will do what you want:-
>
> perl -le '@test=(1,2,3,4,5);print join "\n",@test;'
>
> The -l option ensures a final newline after the last element of the
> array is printed. The order of the options is important as changing
> it to "el" wouldn't work.


If you are going to use the -l switch then just do:

print for @test


:-)

John
--
use Perl;
program
fulfillment

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com