Home > Archive > PERL Beginners > February 2007 > array operation
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]
|
|
| Irfan Sayed 2007-02-26, 3:59 am |
| Hi all,
# /usr/bin/perl
use strict;
use warnings;
my $CT = "/usr/atria/bin/cleartool";
my @test = (1,2,3,4,5);
print "@test\n";
The output of the above script is
1 2 3 4 5
But i need output as
1
2
3
4
5
In short i need to append \n character after every element of the array.
Please help
Regards
Irfan.
| |
| Jeff Pang 2007-02-26, 3:59 am |
|
>
>my $CT = "/usr/atria/bin/cleartool";
>my @test = (1,2,3,4,5);
>print "@test\n";
>
>The output of the above script is
>
> 1 2 3 4 5
>
>But i need output as
>
>1
>2
>3
>4
>5
Hello,
The simple way,
print join "\n",@test;
But when @test is large,using join is not good.
It can write then:
for (@test) {
print $_,"\n";
}
--
Jeff Pang
EMAIL: pangj<at>earthlink.net AIM: jeffpang
| |
| John W. Krahn 2007-02-26, 7:59 am |
| Sayed, Irfan (Irfan) wrote:
> Hi all,
Hello,
> # /usr/bin/perl
> use strict;
> use warnings;
>
> my $CT = "/usr/atria/bin/cleartool";
> my @test = (1,2,3,4,5);
> print "@test\n";
>
> The output of the above script is
>
> 1 2 3 4 5
>
> But i need output as
>
> 1
> 2
> 3
> 4
> 5
>
> In short i need to append \n character after every element of the array.
print map "$_\n", @test;
print "$_\n" for @test;
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
| error79@googlemail.com 2007-02-26, 7:59 am |
| On Feb 26, 9:05 am, isa...@avaya.com (Irfan Sayed) wrote:
> Hi all,
>
> # /usr/bin/perl
> use strict;
> use warnings;
>
> my $CT = "/usr/atria/bin/cleartool";
> my @test = (1,2,3,4,5);
> print "@test\n";
>
> The output of the above script is
>
> 1 2 3 4 5
>
> But i need output as
>
> 1
> 2
> 3
> 4
> 5
>
> In short i need to append \n character after every element of the array.
>
> Please help
>
> Regards
> Irfan.
Hi Irfan
This should do the trick.
=======================
#!/usr/bin/perl -w
use strict;
my @array = (1,2,3,4,5);
print "$_\n" foreach (@array);
=======================
regards,
Marcel
| |
| Arun S 2007-02-26, 7:59 am |
| Hi,
On=202007/02/26,=20at=2010:12,=20John=20W.=20Krahn=20wrote:
=20=20=20=20=20=20print=20map=20"$_\n",=20@test;
=20=20=20=20=20=20print=20"$_\n"=20for=20@test;
Another=20solution=20is=20setting=20the=
20$"=20variable:
my=20@array=20=3D=20qw(1=202=203=204=205
);
{
=A0=A0=20=A0local=20$"=20=3D=20"\n";
=A0=A0=20=A0print=20"@array";
}
The=20above=20code=20does=20not=20seem=2
0to=20work=20for=20me.
I=20get=20an=20error=20saying=20$"=20is=20unrecognised.
do=20i=20need=20to=20include=20anything=
20?
Whilst=20this=20email=20has=20been=20che
cked=20for=20all=20known=20viruses=
,=20recipients=20should=20undertake=20th
eir=20own=20virus=20checking=20as=20=
Xansa=20will=20not=20accept=20any=20liab
ility=20whatsoever.
This=20email=20and=20any=20files=20trans
mitted=20with=20it=20are=20confide=
ntial=20and=20protected=20by=20client=20
privilege.=20=20It=20is=20solely=20=
for=20the=20use=20of=20the=20intended=20
recipient.
Please=20delete=20it=20and=20notify=20th
e=20sender=20if=20you=20have=20rec=
eived=20it=20in
error.=20Unauthorised=20use=20is=20prohibited.
Any=20opinions=20expressed=20in=20this=2
0email=20are=20those=20of=20the=20=
individual=20and=20not
necessarily=20the=20organisation.
=20=20=20=20=20Xansa,=20Registered=20Off
ice:=20420=20Thames=20Valley=20Par=
k=20Drive,
=20=20=20=20=20Thames=20Valley=20Park,=2
0Reading,=20RG6=201PU,=20UK.
=20=20=20=20=20Registered=20in=20England
=20No.1000954.
=20=20=20=20=20t=20=20+44=20(0)8702=2041
6181
=20=20=20=20=20w=20=20www.xansa.com
| |
| Igor Sutton Lopes 2007-02-26, 7:59 am |
| Hi,
On 2007/02/26, at 10:12, John W. Krahn wrote:
> print map "$_\n", @test;
>
>
> print "$_\n" for @test;
Another solution is setting the $" variable:
my @array = qw(1 2 3 4 5);
{
local $" = "\n";
print "@array";
}
HTH!
--
Igor Sutton
igor.sutton@gmail.com
| |
| Igor Sutton Lopes 2007-02-26, 7:59 am |
| Hi,
On 2007/02/26, at 11:08, Arun.S@xansa.com wrote:
>
> The above code does not seem to work for me.
> I get an error saying $" is unrecognised.
> do i need to include anything ?
Actually, I'm using Perl 5.8.8. I don't know if 5.6 or lower has that
variable. Actually, it does. If you are on unix you can try this:
$ perl -e '@a = qw(1 2 3 4); { local $" = "\n"; print "@a" }'
The output will be like this:
1
2
3
4
From "perldoc perlvar":
$LIST_SEPARATOR
$" This is like $, except that it applies to array and
slice val-
ues interpolated into a double-quoted string (or
similar inter-
preted string). Default is a space. (Mnemonic:
obvious, I
think.)
HTH!
--
Igor Sutton
igor.sutton@gmail.com
| |
| doszy@t-online.hu 2007-02-26, 7:59 am |
| Hi,
You may use the $, variable like this:
$, =3D "\n";
print (1,2,3,4);
The output will be:
1
2
3
4
Regards,
Andras
On Mon, 26 Feb 2007 12:27:56 +0100, Igor Sutton Lopes =
<igor.sutton@gmail.com> wrote:
> Hi,
>
> On 2007/02/26, at 11:08, Arun.S@xansa.com wrote:
>
>
> Actually, I'm using Perl 5.8.8. I don't know if 5.6 or lower has that
> variable. Actually, it does. If you are on unix you can try this:
>
> $ perl -e '@a =3D qw(1 2 3 4); { local $" =3D "\n"; print "@a" }'
>
> The output will be like this:
>
> 1
> 2
> 3
> 4
>
> From "perldoc perlvar":
> $LIST_SEPARATOR
> $" This is like $, except that it applies to array and
> slice val-
> ues interpolated into a double-quoted string (or
> similar inter-
> preted string). Default is a space. (Mnemonic:
> obvious, I
> think.)
>
> HTH!
>
> --
> Igor Sutton
> igor.sutton@gmail.com
>
>
>
| |
|
| On Mon, February 26, 2007 4:05 am, Sayed, Irfan \(Irfan\) wrote:
> Hi all,
>
> # /usr/bin/perl
> use strict;
> use warnings;
>
> my $CT = "/usr/atria/bin/cleartool";
> my @test = (1,2,3,4,5);
> print "@test\n";
>
> The output of the above script is
>
> 1 2 3 4 5
>
> But i need output as
>
> 1
> 2
> 3
> 4
> 5
Another way to add to the many:
#!/usr/bin/perl
use strict;
use warnings;
my @test = (1,2,3,4,5);
foreach (@test) {
my $testprint = (<@test> );
print "$testprint\n";
}
| |
| John W. Krahn 2007-02-26, 7:00 pm |
| Paul wrote:
>
> Another way to add to the many:
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my @test = (1,2,3,4,5);
>
> foreach (@test) {
> my $testprint = (<@test> );
> print "$testprint\n";
> }
Really?? Do you actually know what that is doing?
$ perl -MO=Deparse -e'
my @test = (1,2,3,4,5);
foreach (@test) {
my $testprint = (<@test> );
print "$testprint\n";
}
'
my(@test) = (1, 2, 3, 4, 5);
use File::Glob ();
foreach $_ (@test) {
my $testprint = glob(join($", @test));
print "$testprint\n";
}
-e syntax OK
Do you know why that works, or are you just crossing your fingers and hoping
for the best?
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
|
|
|
|
|