Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > a better code by foreach?

Reply
Thread Tools

a better code by foreach?

 
 
Rose
Guest
Posts: n/a
 
      03-07-2008
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";
}


 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      03-07-2008

Quoth "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";
> }


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

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-07-2008
"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]).



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.
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      03-07-2008
On Fri, 7 Mar 2008 21:50:54 +0800 "Rose" <> wrote:

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;
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GL2 better than the XLs? Consumer grade HDs better than pro-sumer Mini DVs? dh@. DVD Video 1 08-28-2008 07:20 PM
The SCO case gets better and better.... thingy NZ Computing 2 12-10-2006 11:33 AM
Is splint really better than lint? Is there a better tool than splint? Peter Bencsik C Programming 2 09-21-2006 10:02 PM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM
Why doesn't the better camera have a better dpi? Tony Carlisle Digital Photography 6 10-04-2003 10:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57