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

Reading partial array

Expand Messages
  • jana_van
    #!/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
    Message 1 of 10 , Jul 26, 2012
    • 0 Attachment
      #!/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
    • Shawn H Corey
      On Thu, 26 Jul 2012 11:57:28 -0000 ... Does this do what you want? #!/usr/bin/env perl use 5.014; use strict; use warnings; use Data::Dumper; # Make
      Message 2 of 10 , Jul 26, 2012
      • 0 Attachment
        On Thu, 26 Jul 2012 11:57:28 -0000
        "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=();
        > }
        >

        Does this do what you want?

        #!/usr/bin/env perl

        use 5.014;
        use strict;
        use warnings;

        use Data::Dumper;

        # Make Data::Dumper pretty
        $Data::Dumper::Sortkeys = 1;
        $Data::Dumper::Indent = 1;

        # Set maximum depth for Data::Dumper, zero means unlimited
        local $Data::Dumper::Maxdepth = 0;

        # configuration values located at the top so they can be easily found
        and changed my $partitions = 4;

        # create an array to test with
        my @arr = ( 0 .. 100 );
        print "\@arr is: @arr\n\n";

        # an AoA
        my @partitions = ();

        # partition @arr into $partitions
        my $step = int( @arr / $partitions );
        for my $n ( 0 .. $partitions-1 ){

        # index to @arr
        my $k = $n;

        # copy the elements to the partition
        while( $k < @arr ){
        push @{ $partitions[$n] }, $arr[$k];
        }continue{
        $k += $partitions;
        }

        }

        print "partitions: ", Dumper \@partitions;



        --
        Just my 0.00000002 million dollars worth,
        Shawn

        Programming is as much about organization and communication
        as it is about coding.

        _Perl links_
        official site : http://www.perl.org/
        beginners' help : http://learn.perl.org/faq/beginners.html
        advance help : http://perlmonks.org/
        documentation : http://perldoc.perl.org/
        news : http://perlsphere.net/
        repository : http://www.cpan.org/
        blog : http://blogs.perl.org/
        regional groups : http://www.pm.org/
      • timothy adigun
        Hello, See my comments below. ... NOTE: The code below, while 25*$k changes for each value of $k, you have 25 constant i.e when $k == 0, your @temp = @arr[0
        Message 3 of 10 , Jul 26, 2012
        • 0 Attachment
          Hello,
          See my comments below.

          On Thu, Jul 26, 2012 at 12:57 PM, 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; ## You want to use feature qw(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++)
          > {
          >
          NOTE: The code below, while 25*$k changes for each value of $k, you have
          25 constant i.e when $k == 0, your @temp = @arr[0 ..25 ] CORRECT
          when $k == 1, your @temp = @arr[25 .. 25 ] Oops # print just 25
          when $k == 2, your @temp = @arr[50 .. 25 ] Oops WRONG !!!. It won't
          work in reverse! That is the error! So, you have "empty" array @temp
          printed!

          > @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.
          >
          > The Code below could do what you wanted:

          #!/usr/bin/perl
          use warnings;
          use strict;

          my @arr = ( 0 .. 100 );
          print "\n The array is @arr \n";

          my $m = 0;
          my $k = 0;
          while ( $k != 4 ) {

          if ( $k < 1 ) {
          ++$m;
          }
          else {
          $m = $k + 1;
          }

          push my @temp, @arr[ ( 25 * $k ) .. ( 25 * $m ) ];
          print "\nArray Fragment $m: @temp\n";
          ++$k;
          }

          Hope this helps


          >
          >



          --
          Tim


          [Non-text portions of this message have been removed]
        • 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 4 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 5 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 6 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 7 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 8 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 9 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 10 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.