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

Re: [PBML] returning data

Expand Messages
  • Gordon Stewart
    ... Also, another question: I m s/// ing a variable, and capturing the data with (.+) . But, when I call it with {$1} (from a hash), it doesn t work. The
    Message 1 of 7 , May 27, 2002
    • 0 Attachment
      >>> burnt_ice091@... 05/28/02 01:48p.m. >>>
      Also, another question: I'm s///'ing a variable, and capturing the data with "(.+)". But, when I call it with {$1} (from a hash), it doesn't work. The debugger says "Error: uninitialized value at line (blah), line (blah); and if the program does work it still displays the same error (while in -w mode), and returns 0 as the `answer`.
      <

      Whats your exact regex used for the above problem ?

      G.
    • burnt_ice091
      ... My exact regex is $_=~ s/(.+)/$my_hash_thing{$1}/go; . It s in a sub, but that shouldn t be any reason for it to not be working. Also, this particular
      Message 2 of 7 , May 27, 2002
      • 0 Attachment
        --- In perl-beginner@y..., "Gordon Stewart" <Gordon.Stewart@i...> wrote:
        >
        >
        > >>> burnt_ice091@y... 05/28/02 01:48p.m. >>>
        > Also, another question: I'm s///'ing a variable, and capturing the data with "(.+)". But, when I call it with {$1} (from a hash), it doesn't work. The debugger says "Error: uninitialized value at line (blah), line (blah); and if the program does work it still displays the same error (while in -w mode), and returns 0 as the `answer`.
        > <
        >
        > Whats your exact regex used for the above problem ?
        >
        > G.

        My exact regex is "$_=~ s/(.+)/$my_hash_thing{$1}/go;". It's in a sub, but that shouldn't be any reason for it to not be working. Also, this particular script worked PERFECTLY until I added a subroutine that runs a help message. If you want a copy of the script, please e-mail me at burnt_ice091@(Remove this part right here)yahoo.com.
      • Gordon Stewart
        ... My exact regex is $_=~ s/(.+)/$my_hash_thing{$1}/go; .
        Message 3 of 7 , May 27, 2002
        • 0 Attachment
          >>> burnt_ice091@... 05/28/02 02:00p.m. >>>
          >
          My exact regex is "$_=~ s/(.+)/$my_hash_thing{$1}/go;".
          <

          as you can see,m im at work - Trying not to email too much..

          why not do :-

          $new = $my_hash_thing{$_};

          All your above line doesm Is get some input, & swap it with the hash value of it..

          (without testing)..

          is that right ?

          then the hash value is stored in the value $new, & you can use it later (you can change $new, to something else if you like... )

          G.
        • burnt_ice091
          ... That s basically what I want it to do (swap). I want to swap a user s character from input (which is contained somewhere in %my_hash_thing), swithch it
          Message 4 of 7 , May 27, 2002
          • 0 Attachment
            --- In perl-beginner@y..., "Gordon Stewart" <Gordon.Stewart@i...> wrote:
            >
            >
            > >>> burnt_ice091@y... 05/28/02 02:00p.m. >>>
            > >
            > My exact regex is "$_=~ s/(.+)/$my_hash_thing{$1}/go;".
            > <
            >
            > as you can see,m im at work - Trying not to email too much..
            >
            > why not do :-
            >
            > $new = $my_hash_thing{$_};
            >
            > All your above line doesm Is get some input, & swap it with the hash value of it..
            >
            > (without testing)..
            >
            > is that right ?
            >
            > then the hash value is stored in the value $new, & you can use it later (you can change $new, to something else if you like... )
            >
            > G.


            That's basically what I want it to do (swap). I want to swap a user's character from input (which is contained somewhere in '%my_hash_thing), swithch it with the corresponding value, regexex it back to $_ (origionally the user's input), and either print or write it out. Also, any help on the first question?
          • Gordon Stewart
            ... That s basically what I want it to do (swap). I want to swap a user s character from input (which is contained somewhere in %my_hash_thing), swithch it
            Message 5 of 7 , May 27, 2002
            • 0 Attachment
              >>> burnt_ice091@... 05/28/02 02:11p.m. >>>

              That's basically what I want it to do (swap). I want to swap a user's character from input (which is contained somewhere in '%my_hash_thing), swithch it with the corresponding value, regexex it back to $_ (origionally the user's input), and either print or write it out. Also, any help on the first question?
              <

              Unfortunatly, Im not too familiar with pop, shift etc..
              I know how to ADD & Subtract values, but i dont know how to store them into a value..

              i',m in a meeting now - bout an hour or so..
              Unless someone helps you before then, I'll try & finish helping your hash problem..


              PS ...

              $hash{1}="apple";
              $hash{2}="pear";


              Basically, If the person enters "pear", you want it to result in '2' or
              f they enter 1, You want "apple" ??

              answer 2 :-

              $_ = $hash{$_};

              answer 1 :-


              $temp=$_;
              foreach $line (keys hash){
              if ($hash{$line} eq $temp){
              $result = $line;
              }
              }


              (I think the "answer 1" is right, havnt tested )

              - Be back later.

              G.
            • Charles K. Clarkson
              ... it?.....). No. ... perl functions are not generally OO. ... Sounds like rotate in assembler. perlfunc describes most of the buitin perl funcions. my
              Message 6 of 7 , May 27, 2002
              • 0 Attachment
                burnt_ice091 <burnt_ice091@...> wrote:

                : What happens to the data that perl 'returns' when you pop or shift?
                : I've tried "pop @array => $variable;", but this does not work (should
                it?.....).

                No.

                : I've also tried ->, but that doesn't work either (damn OOP...).

                perl functions are not generally OO.

                : ANYWAY, how would you call this data back?
                : I would like to pop the first element and then push it to the end.

                Sounds like rotate in assembler. perlfunc describes most of the buitin
                perl funcions.

                my $popped = pop @array;
                my $shifted = shift @array;

                But to just rotate the array to the right or to the left,
                you don't need to pop or shift. You could use an array
                slice. You may be familiar with a slice already. Here's a
                quick reminder. A slice is more than one element:

                my @array = ( 0 .. 9 );
                $array[0]; # first element of the array
                @array[4 ..9] # last 6 elements - an array slice !

                In the array above we could do a rotate with a more complex slice:

                my @array = ( 0 .. 9 );
                @array = @array[ 1 .. 9, 0 ]; # rotate array to the left
                @array = @array[ 9, 0 .. 8 ]; # rotate array to the right

                But what if we don't know the length of the array?

                perl describes the last element of @array with $#array. We
                can create a general case with:

                @array = @array[ 1 .. $#array, 0 ]; # rotate array to the left
                @array = @array[ $#array, 0 .. $#array - 1 ]; # rotate array to the right


                Here are two subroutines I wrote to do this with any number of
                rotations. It defaults to one rotation as above.

                sub rotate_left {
                my( $arr_ref, $rotations ) = @_;
                $rotations ||= 1; # default to 1
                return [ @$arr_ref[ $rotations .. $#$arr_ref, 0 .. $rotations - 1 ] ];
                }

                sub rotate_right {
                my( $arr_ref, $rotations ) = @_;
                $rotations ||= 1; # default to 1
                $rotations = @$arr_ref - $rotations;
                return [ @$arr_ref[ $rotations .. $#$arr_ref, 0 .. $rotations - 1 ] ];
                }

                These accept and return array references to increase speed.

                We could also create routines that operate on the array in-place
                and return the value of the shift (or pop). Here we do it with
                rotate_left. With one rotation it returns a scalar. On a multiple
                rotation it returns an array reference to the values that were
                moved.

                sub rotate_left {
                my( $arr_ref, $rotations ) = @_;
                $rotations ||= 1; # default to 1
                @$arr_ref = @$arr_ref[ $rotations .. $#$arr_ref, 0 .. $rotations - 1 ];
                return $$arr_ref[-1] if $rotations == 1;
                return [ @$arr_ref[ -$rotations .. -1 ] ];
                }

                Here's what I tested with:

                use strict;
                use warnings;
                use Data::Dumper;

                my @array = ( 1 .. 10 );

                print Dumper \@array;
                print Dumper rotate_left(\@array, 4);
                print Dumper \@array;

                sub rotate_left {
                my( $arr_ref, $rotations ) = @_;
                $rotations ||= 1; # default to 1
                @$arr_ref = @$arr_ref[ $rotations .. $#$arr_ref, 0 .. $rotations - 1 ];
                return $$arr_ref[-1] if $rotations == 1;
                return [ @$arr_ref[ -$rotations .. -1 ] ];
                }

                __END__



                HTH,

                Charles K. Clarkson
                --
                Head Bottle Washer,
                Clarkson Energy Homes, Inc.
                Small commercial and residential rehabilitation specialists.
                254 968-8328

                CJ Web Work - Solutions for Real Estate Investors.
                972 216-4952
              • b_harnish
                ... You may be getting that error if $1 is NOT a key in $my_hash_thing. You may be looking for something like this: for my $k (keys %my_hash_thing) { $_ =~
                Message 7 of 7 , May 28, 2002
                • 0 Attachment
                  --- In perl-beginner@y..., "burnt_ice091" <burnt_ice091@y...> wrote:
                  > --- In perl-beginner@y..., "Gordon Stewart" <Gordon.Stewart@i...> wrote:
                  > >
                  > >
                  > > >>> burnt_ice091@y... 05/28/02 01:48p.m. >>>
                  > > Also, another question: I'm s///'ing a variable, and capturing the data with "(.+)". But, when I call it with {$1} (from a hash), it doesn't work. The debugger says "Error: uninitialized value at line (blah), line (blah); and if the program does work it still displays the same error (while in -w mode), and returns 0 as the `answer`.
                  > > <
                  > >
                  > > Whats your exact regex used for the above problem ?
                  > >
                  > > G.
                  >
                  > My exact regex is "$_=~ s/(.+)/$my_hash_thing{$1}/go;". It's in a sub, but that shouldn't be any reason for it to not be working. Also, this particular script worked PERFECTLY until I added a subroutine that runs a help message. If you want a copy of the script, please e-mail me at burnt_ice091@(Remove this part right here)yahoo.com.

                  You may be getting that error if $1 is NOT a key in $my_hash_thing. You may be looking for something like this:
                  for my $k (keys %my_hash_thing) {
                  $_ =~ s/$k/$my_hash_thing{$k}/g;
                  }
                  Where you only want to replace text that exists as keys in %my_hash_thing.

                  - Brian
                Your message has been successfully submitted and would be delivered to recipients shortly.