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

Re: [PBML] move array elements to hash

Expand Messages
  • mablci
    Thanks a lot. I didn t notice that I was adding the stop to my @kv array. -M
    Message 1 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 2 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 3 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 4 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.