Array

V

Ved

Hi All,
I am simply printing as shown in program below.

number of elements in @signal keeps on changing. So how do I maintain
same length in the print statement below.
As of now I keep changing manually.

#!/usr/local/bin/perl
use strict;
use warnings;

my @signal = qw{ mode_20mhz #This keeps on changing
indexEn
txctrl_ltf_fld_index
rotate90
pilot_position
null_position
doneFlag
ltf_sel
};

foreach my $sig (@signal) {
# print "
@signal[0],@signal[1],@signal[2],@signal[3],@signal[4],@signal[5],@signal
[6],@signal[7],@signal[8],@signal[9]; \n";

print "
@signal[0],@signal[1],@signal[2],@signal[3],@signal[4],@signal[5],@signal[6];
\n";
#How do I manage this length of array ?
 
I

Ian Wilson

Ved said:
Hi All,
I am simply printing as shown in program below.

number of elements in @signal keeps on changing. So how do I maintain
same length in the print statement below.
As of now I keep changing manually.

#!/usr/local/bin/perl
use strict;
use warnings;

my @signal = qw{ mode_20mhz #This keeps on changing

and you can't insert #comments into a qw{} construct.
indexEn
txctrl_ltf_fld_index
rotate90
pilot_position
null_position
doneFlag
ltf_sel
};

foreach my $sig (@signal) {
# print "
@signal[0],@signal[1],@signal[2],@signal[3],@signal[4],@signal[5],@signal
[6],@signal[7],@signal[8],@signal[9]; \n";

print "
@signal[0],@signal[1],@signal[2],@signal[3],@signal[4],@signal[5],@signal[6];
\n";

Use a $ not an @ when referencing a scalar element of an array.

print "$signal[0]";
#How do I manage this length of array ?

You can interpolate all the array's values into a string.

print "@signal\n";
 
J

Jens Thoms Toerring

and you can't insert #comments into a qw{} construct.
indexEn
txctrl_ltf_fld_index
rotate90
pilot_position
null_position
doneFlag
ltf_sel
};

foreach my $sig (@signal) {
# print "
@signal[0],@signal[1],@signal[2],@signal[3],@signal[4],@signal[5],@signal
[6],@signal[7],@signal[8],@signal[9]; \n";

print "
@signal[0],@signal[1],@signal[2],@signal[3],@signal[4],@signal[5],@signal[6];
\n";
Use a $ not an @ when referencing a scalar element of an array.
print "$signal[0]";
#How do I manage this length of array ?
You can interpolate all the array's values into a string.
print "@signal\n";

Since Ved seems to want to have commas between the array elements
perhaps

print join( ',', @signal ) . "\n";

would be more to his liking.
Regards, Jens
 
D

Dr.Ruud

Jens Thoms Toerring schreef:
print join( ',', @signal ) . "\n";


There will not be a "\n" at the end.


Alternative-1:

{ local $, = "\n";
print @signal, $,;
}

which has the advantage of not creating a buffer with copies first.


Alternative-2:

print "$_\n" for @signal;

which has the disadvantage of doing multiple prints.


Alternative-3:

print map "$_\n", @signal;

which creates copies, as #2 does, but does a single print.

I prefer #1 especially when the array can be "big" (both in number of
elements as in size per element).
The join-variant is to me the least appealing one.

I suppose that the map-variant makes use of the lazy behaviour of map,
so "underneath" it acts much like the for-variant (it doesn't need to
create a full array first before starting to print).
 
M

Mirco Wahab

Glenn said:
or
{ local ($,,$\)=(',',$/); print @signal }

or

use Perl6::Say;

my @signal = qw # This keeps on changing
' mode_20mhz indexEn txctrl_ltf_fld_indexrotate90
pilot_position null_position doneFlag ltf_sel ';


say() for @signal;


Regards

M.
 
P

Peter Wyzl

Ved said:
Hi All,
I am simply printing as shown in program below.

number of elements in @signal keeps on changing. So how do I maintain
same length in the print statement below.
As of now I keep changing manually.

#!/usr/local/bin/perl
use strict;
use warnings;

my @signal = qw{ mode_20mhz #This keeps on changing
indexEn
txctrl_ltf_fld_index
rotate90
pilot_position
null_position
doneFlag
ltf_sel
};

Find the maximum length $signal[0] is ever likely to be and use defined
string length printf
# assuming 30 is the longest it ever is...

printf "%30s%s%s%s%s%s", $signal[0], $signal[1], $signal[2], $signal[3],
$signal[4], $signal[5];

perldoc -f printf
perldoc -f sprintf (all the formats are documented here)

Though looking at that, since you're typing $signal[x] repetitively, that
might be a clue there is a better way to do that...think loop over the array
of arrays, rather than just the master array.

P
 
D

Dave Weaver

Dr.Ruud said:
Jens Thoms Toerring schreef:


There will not be a "\n" at the end.

??

There is a "\n" when I try it.

% perl
#!/usr/bin/perl
use strict;
use warnings;
my @signal = qw(one two three);
print join( ',', @signal ) . "\n";
print "next line\n";
__END__
one,two,three
next line
%
 
D

Dr.Ruud

Dave Weaver schreef:
Dr.Ruud:

??

There is a "\n" when I try it.

Don't know why, but I guess I was reading

print join( "\n", @signal )

in stead, and worked on from that.


If I would have read properly, I would have ended at

{ local ($\, $,) = ("\n", ','); print @signal }

which is similar to what Glenn replied.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top