Re: Passing two arrays to a subroutine
by tinita (Parson) on Sep 27, 2010 at 11:16 UTC
|
you cannot pass two arrays to a subroutine without loosing the information which entry belongs to which array. a subroutine call flattens array contents to a long list.
best way usually is to pass references of the arrays.
Column(\@act_table, \@colm);
sub Column {
my ($actcol, $col) = @_;
# now iterate over @$col instead of @col
}
perlsub
perlref
update: seems to be a crosspost: http://perlguru.com/gforum.cgi?post=49360 | [reply] [d/l] |
Re: Passing two arrays to a subroutine
by cdarke (Prior) on Sep 27, 2010 at 11:18 UTC
|
Use references. You cannot pass more than one array (or hash) without using a reference. Column(\@act_table,\@colm);
...
sub Column {
no warnings;
my $actcol = shift;
my $col = = shift;
$colno++;
print "In column : $colno\n";
foreach (@$col) {
print "$_\n ";
$_=~s/(<Tc(?:\s?\;?\s?\;?\d?)?>)(.*)/Entity_convert($2)/eg;
$_= ThinSpace($_);
#print "***************** $_\n";
}
}
You are not using @actcol in the subroutine, so why are you passing it? Update: One reason why you might be getting "garbage values first" is because you are starting your array iteration at index 1 instead of zero. | [reply] [d/l] |
|
Thak you so much for your help
However when i am passing arrays like this, It is adding one extra $_ in my array
for ($h=0; $h<=$arg[1]-1; $h++) {
my @colm="";
for ($t=0;$t<=$arg[2]-1;$t++) {
push @colm, $act_table[$t][$h];
print "***##$act_table[$t][$h]\n";
}
Column(\@act_table,\@colm);
}
}
Alnfirstcol (@firstcol); ## Sends first column for alignment to Subro
+utine Alnfirstcol
#foreach my $item (@firstcol) {
#print "$item \n";
#}
## Subroutine : Entity_conv
## Sends cellwise data to Entity module to convert entities
## After entity conversion sends cellwise data ThinSpace subroutine to
+ add thin space wherever required
## Sends column to Text_Nontext subroutine to evaluate whether column
+is contains text data or non text data
sub Column {
no warnings;
my $actcol=shift;
my $col=shift;
$colno++;
foreach (@$col) {
print "$_\n";
$_=~s/(<(?:Tr|Tc)(?:\s?\;?\s?\;?\d?)?>)(.*)/$1.Entity_convert(
+$2)/eg;
}
foreach (@$col) {
#print "After ## $_\n";
}
ThinSpace(\@$col,\@$actcol);
return ();
}
Please let me know if i am doing any mistake
Thank you | [reply] [d/l] |
|
Using references does not change the array in any way. The problem must lie somewhere else - try to print, or better Data::Dumper, the array at various points in the program. BTW, instead of \@$col, you can use just $col.
| [reply] [d/l] [select] |
[Perl 6] Re: Passing two arrays to a subroutine
by moritz (Cardinal) on Sep 27, 2010 at 11:36 UTC
|
Contrary to what the others wrote, you can pass two arrays to a function routine - as long you use Perl 6:
use v6;
my @a = 1, 2, 3;
my @b = <a b c>;
column(@a, @b);
sub column(@first, @second) {
say "First: @first.join(', ')";
say "Second: @second.join(', ')";
}
Perl 6 - links to (nearly) everything that is Perl 6.
| [reply] [d/l] |
|
moritz,
I know you're doing a lot of work on the Perl6 project and keen to raise its profile which is, I'm sure, why you posted that snippet.
Sadly, I'm not sure what it will achieve. I'd grade myself somewhere between senior novice and junior intermediate and that snippet leaves me cold. Many, if not most, Perl5 coders probably have a similar feeling. "So what?", you'd be entitled to ask, and it would be a fair question.
If anyone had a question about a Perl6 script would they come to the Monastery? I can't remember seeing any (but I could be wrong on that).
Anyhows, it may be worth posting a meditation on the relationship between the Monastery and Perl6 to start a discussion on the subject. It is a different language isn't it? I'd be intrested to hear what other monks think. Wouldn't it be best if a Perl6er kicked it off (he says passing the buck)?
Oh, and I can give you a tip for Perl6: Hurry up. :-)
| [reply] |
|
FWIW, I personally find these posted snippets and alternative solutions in Perl 6 interesting. For someone who doesn't have the time or inclination to fully delve into the project (yet), they can serve as a sort of recurring flag, to show that Perl 6 has already achieved much, you can do interesting stuff in it and it's still going ahead.
| [reply] [d/l] |
|
maybe we could use the post title to mark such posts. it might not be interesting to perl5 users, it might also not be interesting to perl 6 users, but it's definitely interesting for those who know perl5 and learn perl 6.
if we mark such posts with something like "perl5 vs perl6" in the title it might become a nice collection of examples what perl5 users want to learn about perl 6.
| [reply] |
|
"So what?", you'd be entitled to ask, and it would be a fair question.
Of course it is a fair question. And the simple answer is that I educate people about Perl. Which is what this site is all about, after all.
If anyone had a question about a Perl6 script would they come to the Monastery? I can't remember seeing any (but I could be wrong on that).
Having tried to answer most of them, I have noticed them. Maybe you need to take a second look, or raise your awareness.
Anyhows, it may be worth posting a meditation on the relationship between the Monastery and Perl6 to start a discussion on the subject.
You mean like Re: What is the scope of Perl Monks?? or maybe New PerlMonks for Perl 6 - A Good Idea?
It is a different language isn't it?
It's a different language than Perl 5, and still Perl.
Perl 6 - links to (nearly) everything that is Perl 6.
| [reply] |
|
I really enjoy moritz's Perl 6 responses. I think they're a practical and effective way to promote the language.
You might even see moritz as a modern day merlyn. :)
You see, twenty years ago merlyn loved Perl 3 so much that he
insisted on answering requests for Unix shell/sed/awk help
with snippets of Perl code.
So much so that many posters resorted to inserting "No Perl please"
in their posts!
As you might expect, this only provoked him further, and
the barrage of Perl 3 snippet responses
intensified.
| [reply] |
|
I don't want to sound jerk, but what's wrong about Mortiz's answer? How many times somebody asks something and we say "use shell script for that", "use this module instead of that other module" or "do it in C" ?
Is up to the person that makes the question to pick the best answer for his/her problem - remember TIMTOWTDI. You can just ignore those answers you don't like.
On the other hand P5 and P6 share *almost* the same community ( fortunately ) so its perfectly fine to me to use the same tools - in this case the perlmonks - to discuss ideas, post snippets and suggest possible solutions.
| [reply] |
|
As someone who had a Perl6 question, I went to IRC and to the Monastery. moritz told me about KeySet, which will be the exact solution to my problem, once it works. In the meantime, I'm using a plain old hash.
In my trivial perl6 sudoku-solver, I do pass multiple arrays into a routine.
On the other hand, while the knowledge that it will be available might raise excitement for the metamorphosis of the rudimentary Perl6 into a hallucinogenic butterfly, it isn't really relevant to this week's requirements, is it?
As Occam said: Entia non sunt multiplicanda praeter necessitatem.
| [reply] |
|
| [reply] |
|
I for one have asked several questions aabout Perl 6 and, so far as I can recall, moritz (among other monks) gave clear and accurate answers to them all. I have seen quite a few questions about Perl 6 from others as well, and probably missed more.
| [reply] |
|
Well I liked the reply from moritz, it was a bit of fun and that's good (I gave him a ribbing on a /msg though).
Back to the question. Correct me if I'm wrong (I know you will), but isn't the Perl 6 solution using references as well? To quote Apocalyse 6: "All positional parameters regardless of their type are considered scalars, and imply scalar context for the actual arguments. If you pass an array or hash to such a parameter, it will actually pass a reference to the array or hash, just as if you'd backslashed the actual argument.
So Perl 6 still passes references, its just that the syntax no longer requires the \.
| [reply] |
|
To quote Apocalyse 6
Please note that the Apocalypses are mostly historical documents. S06 is what you should consider.
but isn't the Perl 6 solution using references as well?
Yes. Under the hood, every argument passing uses references, unless the parameter is decorated with the is copy trait. Unlike in Perl 5, these references are write protected by default.
So Perl 6 still passes references, its just that the syntax no longer requires the \.
Not only the backslash, but also the dereferencing inside the subroutine. Since (nearly) everything in Perl 6 is a reference, references are transparent, and don't require special syntax on either side.
Perl 6 - links to (nearly) everything that is Perl 6.
| [reply] [d/l] |
|
| [reply] |
|
So subroutines are called functions in Perl 6? ;-)
| [reply] |
Re: Passing two arrays to a subroutine
by toolic (Bishop) on Sep 27, 2010 at 12:52 UTC
|
| [reply] |