a better code by foreach?

Discussion in 'Perl Misc' started by Rose, Mar 7, 2008.

  1. Rose

    Rose Guest

    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";
    }
    Rose, Mar 7, 2008
    #1
    1. Advertising

  2. Rose

    Ben Morrow Guest

    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
    Ben Morrow, Mar 7, 2008
    #2
    1. Advertising

  3. Rose

    Guest

    "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.
    , Mar 7, 2008
    #3
  4. Rose

    Ted Zlatanov Guest

    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;
    }
    }
    Ted Zlatanov, Mar 7, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Bencsik
    Replies:
    2
    Views:
    811
  2. Joey Martin
    Replies:
    28
    Views:
    257
    Bob Barrows [MVP]
    Jun 25, 2008
  3. Andrew Thompson
    Replies:
    8
    Views:
    134
    Premshree Pillai
    Jun 7, 2005
  4. Peng Yu
    Replies:
    7
    Views:
    117
    C.DeRykus
    Feb 15, 2011
  5. Replies:
    2
    Views:
    42
    Mark H Harris
    May 13, 2014
Loading...

Share This Page