Click here to get back home

a better code by foreach?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
a better code by foreach? Rose 03-07-2008
Posted by Rose on March 7, 2008, 8:50 am
Please log in for more thread options
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";
}



Posted by Ben Morrow on March 7, 2008, 9:55 am
Please log in for more thread options

> 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 % 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


Posted by xhoster on March 7, 2008, 10:08 am
Please log in for more thread options
> 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.

Posted by Ted Zlatanov on March 7, 2008, 10:57 am
Please log in for more thread options

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->, $notes[0], $start, $end;
}
}


Posted by l v on March 7, 2008, 10:52 pm
Please log in for more thread options
Rose wrote:
> 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";
> }
>
>

Can you explain why and how you are populating %arr1 - %arrn? There may
be a better way to populate %aarX which will make the while { print }
simpler.

--

Len

Similar ThreadsPosted
Code Quality and examples from the Perl source code April 11, 2006, 10:22 am
Separate Javascipt code from pure Perl code May 7, 2007, 3:58 am
foreach vs. for October 24, 2004, 6:18 pm
foreach in my May 18, 2005, 10:40 am
Using foreach?? maybe.. June 3, 2007, 12:30 pm
about foreach September 19, 2007, 5:38 am
How to randomize foreach August 22, 2004, 10:27 am
foreach with two arrays July 19, 2005, 2:35 pm
Why no "perldoc -f for/foreach/while"? February 13, 2006, 8:01 pm
foreach @list ( (1,2), (5,6) ){ ... } May 31, 2006, 10:48 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap