a better code by foreach?

R

Rose

Is it possible for me to use "foreach" to make the following codes in a
better way? I don't want to create subroutine and call. Because I can't use
@%arr this time (%arr1 not equal to %arr[1]). Copying the %arr1, %arr2, ...,
%arrn into a single large array is impractical, because %arr1, ...n is
already very large each.


while (($loc, $sub) = each(%arr1)) {
print "$loc\tSub\t$sub\t$notes[0]\t$start\t$end\n";
}

while (($loc, $sub) = each(%arr2)) {
print "$loc\tSub\t$sub\t$notes[0]\t$start\t$end\n";
}

....

while (($loc, $sub) = each(%arrn)) {
print "$loc\tSub\t$sub\t$notes[0]\t$start\t$end\n";
}
 
B

Ben Morrow

Quoth "Rose said:
Is it possible for me to use "foreach" to make the following codes in a
better way? I don't want to create subroutine and call. Because I can't use
@%arr this time (%arr1 not equal to %arr[1]). Copying the %arr1, %arr2, ...,
%arrn into a single large array is impractical, because %arr1, ...n is
already very large each.

while (($loc, $sub) = each(%arr1)) {
print "$loc\tSub\t$sub\t$notes[0]\t$start\t$end\n";
}

Making an array out of several hashrefs doesn't copy the contents, so
you can perfectly well just do

my @arr = (\%arr1, \%arr2, ...);

and perl will only allocate a small (proportional to the number of
hashes, not to the size of their contents) amount of memory.

However, you are going about this the wrong way. Whenever you have
variables named like that, it is a hint you should have used an array to
start with. Go back to the code that populates %arrn, and change it to
use %{$arr[n]} instead. Chances are you can rewrite it to use for loops
in a similar way. Then you can do

foreach my $hash (@arr) {
while (my ($loc, $sub) = each %$hash) {
...
}
}

You are using 'strict', aren't you?

Ben
 
X

xhoster

Rose said:
Is it possible for me to use "foreach" to make the following codes in a
better way? I don't want to create subroutine and call. Because I can't
use @%arr this time (%arr1 not equal to %arr[1]).


Perhaps you have written something in some other thread that I haven't
read that if I had read would make the above make sense, but otherwise
the above doesn't make sense.
Copying the %arr1,
%arr2, ..., %arrn into a single large array is impractical, because
%arr1, ...n is already very large each.

@arrn = \(%arr1, %arr2, %arr3, %arr4);

This just creates references to each hash. The data in the hashes is not
copied, it just has two paths to get to it, through the old name and
through the new reference. But it would better to just work with @arrn
from the start and never have %arr1 to start with.


while (($loc, $sub) = each(%arr1)) {
print "$loc\tSub\t$sub\t$notes[0]\t$start\t$end\n";
}

while (($loc, $sub) = each(%arr2)) {
print "$loc\tSub\t$sub\t$notes[0]\t$start\t$end\n";
}

foreach my $ref (@arrn) {
while (($loc, $sub) = each(%$ref)) {
print "$loc\tSub\t$sub\t$notes[0]\t$start\t$end\n";
}
};



Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
T

Ted Zlatanov

R> Is it possible for me to use "foreach" to make the following codes in a
R> better way? I don't want to create subroutine and call. Because I can't use
R> @%arr this time (%arr1 not equal to %arr[1]). Copying the %arr1, %arr2, ...,
R> %arrn into a single large array is impractical, because %arr1, ...n is
R> already very large each.


R> while (($loc, $sub) = each(%arr1)) {
R> print "$loc\tSub\t$sub\t$notes[0]\t$start\t$end\n";
R> }

R> while (($loc, $sub) = each(%arr2)) {
R> print "$loc\tSub\t$sub\t$notes[0]\t$start\t$end\n";
R> }

R> ...

R> while (($loc, $sub) = each(%arrn)) {
R> print "$loc\tSub\t$sub\t$notes[0]\t$start\t$end\n";
R> }

Untested code. I like printf() but the print() works fine too. See
`perldoc perlref' for further information.

# you can automate this assignment too, but it may not make sense in
# your application's context
my @arrays = (\%arr1, \%arr2 ... \%arrn);

foreach my $array (@arrays)
{
foreach my $loc (keys %$array)
{
printf "%s\tSub\t%s\t%s\t%s\t%s\n",
$loc, $array->{$loc}, $notes[0], $start, $end;
}
}
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top