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

Re: [PBML] move array elements to hash

Expand Messages
  • merlyn@stonehenge.com
    ... mablci last if ($kv == stop ); This is a numeric comparison with something that is numerically a 0 . Did you want that, or perhaps an eq instead of
    Message 1 of 8 , Apr 14, 2011
    • 0 Attachment
      >>>>> "mablci" == mablci <mablci@...> writes:

      mablci> last if ($kv == "stop");

      This is a numeric comparison with something that is numerically a "0".
      Did you want that, or perhaps an "eq" instead of "=="?

      mablci> %hash = $kv[$i] => $kv[$i + 1];

      This overwrites the entire contents of the hash with a single element
      consisting of adjacent elements of the @kv array. Did you want that?

      Both of these are covered in Learning Perl. Do you have a copy of
      Learning Perl? If so, please read it. If not, get it, and see above.

      --
      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
    • Alan Haggai Alavi
      Hello M, Adding to what Randal already mentioned, you should pop the stop out of @kv immediately after the following loop: =pod code while ($kv = ) {
      Message 2 of 8 , Apr 14, 2011
      • 0 Attachment
        Hello M,

        Adding to what Randal already mentioned, you should pop the 'stop' out
        of @kv immediately after the following loop:

        =pod code

        while ($kv = <STDIN>) {
        chomp($kv);
        push(@kv, $kv);
        last if ($kv == "stop");
        }

        =cut

        Regards,
        Alan Haggai Alavi.
        --
        The difference makes the difference
      • mablci
        ... Thanks a lot. That pointed me in the right direction. -M
        Message 3 of 8 , Apr 15, 2011
        • 0 Attachment
          >This is a numeric comparison with something that is numerically >a "0".
          > Did you want that, or perhaps an "eq" instead of "=="?
          > mablci> %hash = $kv[$i] => $kv[$i + 1];
          > This overwrites the entire contents of the hash with a single > element consisting of adjacent elements of the @kv array. Did you > want that?

          > Both of these are covered in Learning Perl. Do you have a copy of
          > Learning Perl?


          Thanks a lot.
          That pointed me in the right direction.
          -M

          --- In perl-beginner@yahoogroups.com, merlyn@... wrote:
          >
          > >>>>> "mablci" == mablci <mablci@...> writes:
          >
          > mablci> last if ($kv == "stop");
          >
          > This is a numeric comparison with something that is numerically a "0".
          > Did you want that, or perhaps an "eq" instead of "=="?
          >
          > mablci> %hash = $kv[$i] => $kv[$i + 1];
          >
          > This overwrites the entire contents of the hash with a single element
          > consisting of adjacent elements of the @kv array. Did you want that?
          >
          > Both of these are covered in Learning Perl. Do you have a copy of
          > Learning Perl? If so, please read it. If not, get it, and see above.
          >
          > --
          > 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
          >
        • mablci
          Thanks a lot. I didn t notice that I was adding the stop to my @kv array. -M
          Message 4 of 8 , Apr 15, 2011
          • 0 Attachment
            Thanks a lot.
            I didn't notice that I was adding the "stop" to my
            @kv array.

            -M
            --- In perl-beginner@yahoogroups.com, Alan Haggai Alavi <alanhaggai@...> wrote:
            >
            > Hello M,
            >
            > Adding to what Randal already mentioned, you should pop the 'stop' out
            > of @kv immediately after the following loop:
            >
            > =pod code
            >
            > while ($kv = <STDIN>) {
            > chomp($kv);
            > push(@kv, $kv);
            > last if ($kv == "stop");
            > }
            >
            > =cut
            >
            > Regards,
            > Alan Haggai Alavi.
            > --
            > The difference makes the difference
            >
          • Charles K. Clarkson
            ... Or just don t add stop in the first place: while ( my $kv = ) { chomp $kv; last if $kv eq stop ; push @kv, $kv; } Charles Clarkson -- Mobile
            Message 5 of 8 , Apr 15, 2011
            • 0 Attachment
              On 4/14/2011 9:58 PM, Alan Haggai Alavi wrote:

              > Hello M,
              >
              > Adding to what Randal already mentioned, you should pop the 'stop' out
              > of @kv immediately after the following loop:
              >
              > =pod code
              >
              > while ($kv =<STDIN>) {
              > chomp($kv);
              > push(@kv, $kv);
              > last if ($kv == "stop");
              > }

              Or just don't add 'stop' in the first place:

              while ( my $kv = <STDIN> ) {

              chomp $kv;

              last if $kv eq 'stop';

              push @kv, $kv;
              }



              Charles Clarkson
              --
              Mobile Home Investor
              Free Market Advocate
              Programmer

              I'm not really a smart person. I just play one on the Internet.

              Stephenville, TX
              http://twitter.com/CharlesClarkson
              +1 (254) 968-8328
            • Edward Willekens
              #!/usr/bin/perl -w use strict; my %hash; my $kv; my @kv; my @v; my $i; my $key; my $value; #create key - value pairs to go into a hash by first entering each
              Message 6 of 8 , Apr 16, 2011
              • 0 Attachment
                #!/usr/bin/perl -w
                use strict;

                my %hash;
                my $kv;
                my @kv;
                my @v;
                my $i;
                my $key;
                my $value;

                #create key - value pairs to go into a hash by first entering each
                #into a list @k or @v
                print "input key/value pairs: first a key then return, then a value
                then return, etc. To stop entering key/value pairs type 'stop'\n";
                # the following statement allows for termination of input by hitting cntl 'd'
                while ($kv = <STDIN>) {
                chomp($kv);
                push(@kv, $kv);
                # string compare
                last if ($kv =~ /^stop$/);
                }

                # move each key or value located at each index of array @kv into a
                # hash so that they pair up e.g. $kv[i] with $kv[i +1] etc.

                #ignore the last element (it contains "stop")
                for($i = 0; $i < $#kv; $i++) {
                #assign the value to the hash variable
                $hash{$kv[$i]}=$kv[$i + 1];
                $i++;
                }

                while ( ($key,$value) = each %hash ) {
                print "$key +> $value\n";
                }

                --- On Thu, 4/14/11, mablci <mablci@...> wrote:

                From: mablci <mablci@...>
                Subject: [PBML] move array elements to hash
                To: perl-beginner@yahoogroups.com
                Date: Thursday, April 14, 2011, 5:27 PM







                 









                Hi,

                I am trying to move array elements (populated from the <STDIN>) into a

                hash as pairs [i] and [i + 1] and then print them out using the code below. If I enter "bob" as the first element and hit enter I get the

                error messages below. I guess there are multiple problems with my

                code. For one it appears that my $kv variable is supposed to take numeric values. I'm not sure if this is the reason the program aborts once I hit enter after my first entry (i.e. "bob").

                Any hints on why I my $kv varible is expected to be numeric

                or any other obvious problems with my code?



                Thanks,

                M



                Useless use of array element in void context at L2_Q9.pl line 23.

                input key/value pairs: first a key then return, then a value then

                return, etc. To stop entering key/value pairs type 'stop'

                jim

                Argument "stop" isn't numeric in numeric eq (==) at L2_Q9.pl line 17,<STDIN> line 1.

                Argument "jim" isn't numeric in numeric eq (==) at L2_Q9.pl line 17,

                <STDIN> line 1.

                Odd number of elements in hash assignment at L2_Q9.pl line 23, <STDIN>

                line 1.

                Use of uninitialized value in concatenation (.) or string at L2_Q9.pl

                line 28, <STDIN> line 1.

                jim +>



                #!/usr/bin/perl -w

                use strict;



                my %hash;

                my $kv;

                my @kv;

                my @v;

                my $i;

                my $key;

                my $value;



                #create key - value pairs to go into a hash by first entering each

                #into a list @k or @v

                print "input key/value pairs: first a key then return, then a value

                then return, etc. To stop entering key/value pairs type 'stop'\n";

                while ($kv = <STDIN>) {

                chomp($kv);

                push(@kv, $kv);

                last if ($kv == "stop");

                }



                # move each key or value located at each index of array @kv into a

                # hash so that they pair up e.g. $kv[i] with $kv[i +1] etc.



                for($i = 0; $i < $#kv + 1; $i++) {

                %hash = $kv[$i] => $kv[$i + 1];

                $i++;

                }



                while ( ($key, $value) = each %hash ) {

                print "$key +> $value\n";

                }






















                [Non-text portions of this message have been removed]
              • merlyn@stonehenge.com
                ... Edward my %hash; Edward my $kv; Edward my @kv; Edward my @v; Edward my $i; Edward my $key; Edward my $value; I stopped reading here. Never
                Message 7 of 8 , Apr 16, 2011
                • 0 Attachment
                  >>>>> "Edward" == Edward Willekens <edwardwillekens@...> writes:

                  Edward> my %hash;
                  Edward> my $kv;
                  Edward> my @kv;
                  Edward> my @v;
                  Edward> my $i;
                  Edward> my $key;
                  Edward> my $value;

                  I stopped reading here. Never introduce your variables until you need
                  them. Creating them with a null value is bad practice.

                  --
                  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
                Your message has been successfully submitted and would be delivered to recipients shortly.