Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Reading partial array

Expand Messages
  • merlyn@stonehenge.com
    ... jana use Switch; Please don t. jana My requirement is that @temp should print 25 elments of @arr for jana each iteration and the original array @arr
    Message 1 of 10 , Jul 30, 2012
    • 0 Attachment
      >>>>> "jana" == jana van <jana_van@...> writes:

      jana> use Switch;

      Please don't.

      jana> My requirement is that @temp should print 25 elments of @arr for
      jana> each iteration and the original array @arr should be
      jana> intact. Please help here.

      The general pattern is this:

      my @copy = @arr;
      while (@copy) {
      my @temp = splice(@copy, 0, 25);
      print "@temp\n"; # or whatever
      }

      --
      Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
      <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
      Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
      See http://methodsandmessages.posterous.com/ for Smalltalk discussion
    • Rob Richardson
      Randal, Why not use Switch? Since this is a beginner s forum, an explanation would be helpful. RobR
      Message 2 of 10 , Jul 30, 2012
      • 0 Attachment
        Randal,

        Why not use Switch?
        Since this is a beginner's forum, an explanation would be helpful.

        RobR



        On Mon, Jul 30, 2012 at 11:44 AM, <merlyn@...> wrote:
        >>>>>> "jana" == jana van <jana_van@...> writes:
        >
        > jana> use Switch;
        >
        > Please don't.
      • Matthew K
        The author of Switch posted this. http://www.perlmonks.org/?node_id=486756 For those of you who don t want to click, he lists it under, modules you shouldn t
        Message 3 of 10 , Jul 30, 2012
        • 0 Attachment
          The author of Switch posted this.

          http://www.perlmonks.org/?node_id=486756


          For those of you who don't want to click, he lists it under, "modules you shouldn't use in production because their purpose is to explore and prototype future core language features".


          --
          Matthew Kunzman
          Software Engineer 
          Boise, ID USA


          Email: matt_hew@... 
          LinkedIn: http://www.linkedin.com/pub/matthew-kunzman/b/5ba/94a
          -------------------------------------------------------------------------


          >________________________________
          > From: "merlyn@..." <merlyn@...>
          >To: jana_van <jana_van@...>
          >Cc: perl-beginner@yahoogroups.com
          >Sent: Monday, July 30, 2012 9:44 AM
          >Subject: Re: [PBML] Reading partial array
          >
          >

          >>>>>> "jana" == jana van <jana_van@...> writes:
          >
          >jana> use Switch;
          >
          >Please don't.
          >
          >jana> My requirement is that @temp should print 25 elments of @arr for
          >jana> each iteration and the original array @arr should be
          >jana> intact. Please help here.
          >
          >The general pattern is this:
          >
          >my @copy = @arr;
          >while (@copy) {
          >my @temp = splice(@copy, 0, 25);
          >print "@temp\n"; # or whatever
          >}
          >
          >--
          >Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
          ><merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
          >Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
          >See http://methodsandmessages.posterous.com/ for Smalltalk discussion
          >
          >
          >
          >

          [Non-text portions of this message have been removed]
        • Shlomi Fish
          Hi Rob, On Mon, 30 Jul 2012 12:36:02 -0400 ... Reading from http://perl-begin.org/tutorials/bad-elements/#switch_pm :
          Message 4 of 10 , Jul 30, 2012
          • 0 Attachment
            Hi Rob,

            On Mon, 30 Jul 2012 12:36:02 -0400
            Rob Richardson <CedricCicada@...> wrote:

            > Randal,
            >
            > Why not use Switch?
            > Since this is a beginner's forum, an explanation would be helpful.
            >

            Reading from http://perl-begin.org/tutorials/bad-elements/#switch_pm :

            <<<
            One should not use Switch.pm to implement a switch statement because it's a source filter, tends to break a lot of code, and causes unexpected problems. Instead one should either use given/when, which are only available in perl-5.10 and above, or dispatch tables, or alterantively plain if/elsif/else structures.
            >>>

            Regards,

            Shlomi Fish

            > RobR
            >
            >
            >
            > On Mon, Jul 30, 2012 at 11:44 AM, <merlyn@...> wrote:
            > >>>>>> "jana" == jana van <jana_van@...> writes:
            > >
            > > jana> use Switch;
            > >
            > > Please don't.



            --
            -----------------------------------------------------------------
            Shlomi Fish http://www.shlomifish.org/
            Stop Using MSIE - http://www.shlomifish.org/no-ie/

            Better be a tail for the lions, than the head of the jackals.
            — Pirkei Avot, 4 15

            Please reply to list if it's a mailing list post - http://shlom.in/reply .
          • BhavinP
            ... my @arr = (1..100); my $start = 0; my $end = 0; my $incr = 25; sub get25{ $end = $start + $incr; my @retarr = @arr[$start..$end]; $start = $end; return
            Message 5 of 10 , Sep 20, 2012
            • 0 Attachment
              --- In perl-beginner@yahoogroups.com, "jana_van" <jana_van@...> wrote:
              >
              > #!/usr/local/roadm/bin/perl
              > # This is compiled with threading support
              > use strict;
              > use warnings;
              > use threads;
              > use threads::shared;
              > use Switch;
              >
              > my @arr=();
              >
              > my $k=0;
              >
              > for($k=0;$k<100;$k++)
              > {
              > push(@arr, $k);
              > }
              >
              > print "\n The array is ", "@arr", "\n";
              > my @temp=();
              >
              > for($k=0; $k<4;$k++)
              > {
              > @temp = @arr[25*$k .. 25];
              > print "\n The fragmented array is ", "@temp", "\n";
              > @temp=();
              > }
              >
              > In the second for loop, for the first iteration @temp reading 25 elements but for the second iteration onwards the values read by @temp are not as expected. whats the fault here ?
              >
              > My requirement is that @temp should print 25 elments of @arr for each iteration and the original array @arr should be intact. Please help here.
              >
              > Thanks
              >



              my @arr = (1..100);
              my $start = 0;
              my $end = 0;
              my $incr = 25;

              sub get25{
              $end = $start + $incr;
              my @retarr = @arr[$start..$end];
              $start = $end;
              return \@retarr;

              }
            • Joe Pepersack
              There are a couple problems with the solution provided by BhavinP, not the least of which is that it doesn t duplicate Jana s requirements. The subroutine is
              Message 6 of 10 , Sep 23, 2012
              • 0 Attachment
                There are a couple problems with the solution provided by BhavinP, not
                the least of which is that it doesn't duplicate Jana's requirements.
                The subroutine is never invoked, and there is a completely useless
                assignment after the slice.

                In Jana's example, there's no need to use threads or Switch, as the code
                doesn't use those features. Don't use a module or feature that's never
                invoked.

                The first part of your code is easy. Rather than using a for loop to
                assign 0 to 99 to an array, we can use the range (..) operator:

                my @arr = (0..99);

                It's more compact and faster.

                The reason your code isn't working is that you're mis-using the range
                operator in your array slice.
                You have:
                @temp = @arr[25*$k .. 25];

                You should have
                @temp = @arr[ ($k*25) .. (($k*25)+24) ];

                When doing an array slice, you are telling it the values of the first
                and second array indexes, not the first index and the number of values
                you want. We also have to remember that array indexes are zero-based,
                so we need to adjust accordingly to avoid off-by-one / fencepost
                errors. Doing the math inline makes the code hard to read, so for clarity:

                my $incr = 25;
                for my $k ( 0 .. 3 ) {
                my $start = $incr * $k;
                my $end = $start + ($incr - 1); # parens for clarity
                my @temp = @arr[ $start .. $end ];
                print "Array slice $start to $end is ( @temp )\n";
                }

                This will give us:
                Array slice 0 to 24 is ( ... )
                Array slice 25 to 49 is ( ... )
                Array slice 50 to 74 is ( ... )
                Array slice 75 to 99 is ( ... )

                Printing the values of $start and $end allows us to easily debug the
                code. If we had, for instance, said:
                $end = $start + $incr;
                it would be immediately obvious what the problem was.

                For style and additional clarity it's probably a good idea factor this
                logic out into a function, but that's another lesson. You also
                generally want to try to avoid using C-style for loops. Making your code
                clear and readable should always be a priority.

                On 9/20/12 10:41 AM, BhavinP wrote:
                >
                >
                >
                > --- In perl-beginner@yahoogroups.com
                > <mailto:perl-beginner%40yahoogroups.com>, "jana_van" <jana_van@...> wrote:
                > >
                > > #!/usr/local/roadm/bin/perl
                > > # This is compiled with threading support
                > > use strict;
                > > use warnings;
                > > use threads;
                > > use threads::shared;
                > > use Switch;
                > >
                > > my @arr=();
                > >
                > > my $k=0;
                > >
                > > for($k=0;$k<100;$k++)
                > > {
                > > push(@arr, $k);
                > > }
                > >
                > > print "\n The array is ", "@arr", "\n";
                > > my @temp=();
                > >
                > > for($k=0; $k<4;$k++)
                > > {
                > > @temp = @arr[25*$k .. 25];
                > > print "\n The fragmented array is ", "@temp", "\n";
                > > @temp=();
                > > }
                > >
                > > In the second for loop, for the first iteration @temp reading 25
                > elements but for the second iteration onwards the values read by @temp
                > are not as expected. whats the fault here ?
                > >
                > > My requirement is that @temp should print 25 elments of @arr for
                > each iteration and the original array @arr should be intact. Please
                > help here.
                > >
                > > Thanks
                > >
                >
                > my @arr = (1..100);
                > my $start = 0;
                > my $end = 0;
                > my $incr = 25;
                >
                > sub get25{
                > $end = $start + $incr;
                > my @retarr = @arr[$start..$end];
                > $start = $end;
                > return \@retarr;
                >
                > }
                >
                >




                [Non-text portions of this message have been removed]
              • BhavinP
                well, my code was just to point in the right direction. a module that will give you chunks of 25 elements from the array each time its invoked. the last
                Message 7 of 10 , Sep 24, 2012
                • 0 Attachment
                  well, my code was just to point in the right direction. a module that will give you chunks of 25 elements from the array each time its invoked. the last statement after we got the chunk is to move the array pointer 25 elements forward.

                  ofcourse , we need to wrap it in a loop like map { print Dumper get25(); } (1..3) in one line would do it...


                  --- In perl-beginner@yahoogroups.com, Joe Pepersack <joe@...> wrote:
                  >
                  > There are a couple problems with the solution provided by BhavinP, not
                  > the least of which is that it doesn't duplicate Jana's requirements.
                  > The subroutine is never invoked, and there is a completely useless
                  > assignment after the slice.
                  >
                  > In Jana's example, there's no need to use threads or Switch, as the code
                  > doesn't use those features. Don't use a module or feature that's never
                  > invoked.
                  >
                  > The first part of your code is easy. Rather than using a for loop to
                  > assign 0 to 99 to an array, we can use the range (..) operator:
                  >
                  > my @arr = (0..99);
                  >
                  > It's more compact and faster.
                  >
                  > The reason your code isn't working is that you're mis-using the range
                  > operator in your array slice.
                  > You have:
                  > @temp = @arr[25*$k .. 25];
                  >
                  > You should have
                  > @temp = @arr[ ($k*25) .. (($k*25)+24) ];
                  >
                  > When doing an array slice, you are telling it the values of the first
                  > and second array indexes, not the first index and the number of values
                  > you want. We also have to remember that array indexes are zero-based,
                  > so we need to adjust accordingly to avoid off-by-one / fencepost
                  > errors. Doing the math inline makes the code hard to read, so for clarity:
                  >
                  > my $incr = 25;
                  > for my $k ( 0 .. 3 ) {
                  > my $start = $incr * $k;
                  > my $end = $start + ($incr - 1); # parens for clarity
                  > my @temp = @arr[ $start .. $end ];
                  > print "Array slice $start to $end is ( @temp )\n";
                  > }
                  >
                  > This will give us:
                  > Array slice 0 to 24 is ( ... )
                  > Array slice 25 to 49 is ( ... )
                  > Array slice 50 to 74 is ( ... )
                  > Array slice 75 to 99 is ( ... )
                  >
                  > Printing the values of $start and $end allows us to easily debug the
                  > code. If we had, for instance, said:
                  > $end = $start + $incr;
                  > it would be immediately obvious what the problem was.
                  >
                  > For style and additional clarity it's probably a good idea factor this
                  > logic out into a function, but that's another lesson. You also
                  > generally want to try to avoid using C-style for loops. Making your code
                  > clear and readable should always be a priority.
                  >
                  > On 9/20/12 10:41 AM, BhavinP wrote:
                  > >
                  > >
                  > >
                  > > --- In perl-beginner@yahoogroups.com
                  > > <mailto:perl-beginner%40yahoogroups.com>, "jana_van" <jana_van@> wrote:
                  > > >
                  > > > #!/usr/local/roadm/bin/perl
                  > > > # This is compiled with threading support
                  > > > use strict;
                  > > > use warnings;
                  > > > use threads;
                  > > > use threads::shared;
                  > > > use Switch;
                  > > >
                  > > > my @arr=();
                  > > >
                  > > > my $k=0;
                  > > >
                  > > > for($k=0;$k<100;$k++)
                  > > > {
                  > > > push(@arr, $k);
                  > > > }
                  > > >
                  > > > print "\n The array is ", "@arr", "\n";
                  > > > my @temp=();
                  > > >
                  > > > for($k=0; $k<4;$k++)
                  > > > {
                  > > > @temp = @arr[25*$k .. 25];
                  > > > print "\n The fragmented array is ", "@temp", "\n";
                  > > > @temp=();
                  > > > }
                  > > >
                  > > > In the second for loop, for the first iteration @temp reading 25
                  > > elements but for the second iteration onwards the values read by @temp
                  > > are not as expected. whats the fault here ?
                  > > >
                  > > > My requirement is that @temp should print 25 elments of @arr for
                  > > each iteration and the original array @arr should be intact. Please
                  > > help here.
                  > > >
                  > > > Thanks
                  > > >
                  > >
                  > > my @arr = (1..100);
                  > > my $start = 0;
                  > > my $end = 0;
                  > > my $incr = 25;
                  > >
                  > > sub get25{
                  > > $end = $start + $incr;
                  > > my @retarr = @arr[$start..$end];
                  > > $start = $end;
                  > > return \@retarr;
                  > >
                  > > }
                  > >
                  > >
                  >
                  >
                  >
                  >
                  > [Non-text portions of this message have been removed]
                  >
                Your message has been successfully submitted and would be delivered to recipients shortly.