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

Re: [PBML] Variable Interpolation

Expand Messages
  • merlyn@stonehenge.com
    ... mail Hi Friends, mail Can you please let me know how to get the content of a value of the mail variable . mail For Eg: mail my $type = shift; mail
    Message 1 of 20 , Jan 2, 2006
    • 0 Attachment
      >>>>> "mail" == mail meda <mailmeda@...> writes:

      mail> Hi Friends,
      mail> Can you please let me know how to get the content of a " value of the
      mail> variable ".
      mail> For Eg:

      mail> my $type = shift;
      mail> my $f_group='PBML';
      mail> my $f_lang='perl';
      mail> my $f_os='linux';
      mail> my $ref = "f_$type";

      In Perl, the way to do this is to put the elements in a hash:

      my %mapping = (group => 'PBML', lang => 'perl', os => 'linux');
      my $type = shift;
      my $result = $mapping{$type};

      --
      Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
      <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
      Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
      See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
    • KalyanRaj
      hi , as mentioned by Randal, we can put elements in a hash... shift is a keyword in Perl which gives out first element in an array...I wonder is there
      Message 2 of 20 , Jan 2, 2006
      • 0 Attachment
        hi ,

        as mentioned by Randal, we can put elements in a hash...
        "shift" is a keyword in Perl which gives out first element in an array...I
        wonder is there anyother use of "shift"
        when i'm executing the code given by Randal, nothing is printed out....

        MailMeda --- the below code print out PBML which is what you wanted. Also
        checkout the scope of the variables. if we place the statement
        print $result outside reference subroutine, it wont print anything.

        my %mapping = (group => 'PBML', lang => 'perl', os => 'linux');
        reference(group);
        sub reference {
        my $type = shift;
        my $result = $mapping{$type};
        print $result;
        }

        Regards,
        KalyanRaj

        -----Original Message-----
        From: perl-beginner@yahoogroups.com
        [mailto:perl-beginner@yahoogroups.com]On Behalf Of merlyn@...
        Sent: Monday, January 02, 2006 6:26 PM
        To: mail meda
        Cc: perl-beginner@yahoogroups.com
        Subject: Re: [PBML] Variable Interpolation


        >>>>> "mail" == mail meda <mailmeda@...> writes:

        mail> Hi Friends,
        mail> Can you please let me know how to get the content of a " value of the
        mail> variable ".
        mail> For Eg:

        mail> my $type = shift;
        mail> my $f_group='PBML';
        mail> my $f_lang='perl';
        mail> my $f_os='linux';
        mail> my $ref = "f_$type";

        In Perl, the way to do this is to put the elements in a hash:

        my %mapping = (group => 'PBML', lang => 'perl', os => 'linux');
        my $type = shift;
        my $result = $mapping{$type};

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
        See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl
        training!


        Unsubscribing info is here:
        http://help.yahoo.com/help/us/groups/groups-32.html
        Yahoo! Groups Links
      • Shawn Corey
        ... You should always use an argument for shift. In this case, shift works on @_ since it is inside a subroutine. Outside any subroutine, shift works on @ARGV.
        Message 3 of 20 , Jan 2, 2006
        • 0 Attachment
          KalyanRaj wrote:
          > as mentioned by Randal, we can put elements in a hash...
          > "shift" is a keyword in Perl which gives out first element in an array...I
          > wonder is there anyother use of "shift"
          > when i'm executing the code given by Randal, nothing is printed out....
          >
          > MailMeda --- the below code print out PBML which is what you wanted. Also
          > checkout the scope of the variables. if we place the statement
          > print $result outside reference subroutine, it wont print anything.
          >
          > my %mapping = (group => 'PBML', lang => 'perl', os => 'linux');
          > reference(group);
          > sub reference {
          > my $type = shift;
          > my $result = $mapping{$type};
          > print $result;
          > }

          You should always use an argument for shift. In this case, shift works
          on @_ since it is inside a subroutine. Outside any subroutine, shift
          works on @ARGV. To avoid confusion, always supply it with an argument.

          my $type = shift @_;


          --

          Just my 0.00000002 million dollars worth,
          --- Shawn

          "Probability is now one. Any problems that are left are your own."
          SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

          * Perl tutorials at http://perlmonks.org/?node=Tutorials
          * A searchable perldoc is available at http://perldoc.perl.org/
        • merlyn@stonehenge.com
          ... Shawn You should always use an argument for shift. That s a bit strong. It s accepted to leave the argument off shift when it s being used in a sane way.
          Message 4 of 20 , Jan 2, 2006
          • 0 Attachment
            >>>>> "Shawn" == Shawn Corey <shawn.corey@...> writes:

            Shawn> You should always use an argument for shift.

            That's a bit strong. It's accepted to leave the argument off shift
            when it's being used in a sane way. I presumed the original poster
            knew what he was doing, and didn't comment on that.

            --
            Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
            <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
            Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
            See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
          • Shawn Corey
            ... Yes, it s strong but all good discipline seems strong until you reap the benefits from it. I don t think it s sane for the implied behaviour of any
            Message 5 of 20 , Jan 2, 2006
            • 0 Attachment
              Randal L. Schwartz wrote:
              >>>>>>"Shawn" == Shawn Corey <shawn.corey@...> writes:
              >
              >
              > Shawn> You should always use an argument for shift.
              >
              > That's a bit strong. It's accepted to leave the argument off shift
              > when it's being used in a sane way. I presumed the original poster
              > knew what he was doing, and didn't comment on that.
              >

              Yes, it's strong but all good discipline seems strong until you reap the
              benefits from it. I don't think it's sane for the implied behaviour of
              any construct to change. What it means is that when I'm reading a
              program, I have one more thing to keep track of. Anything that reduces
              this overhead is worth the effort.

              Always write your programs as though you aren't going to see them again
              for 25 years and then have to make a critical change immediately.


              --

              Just my 0.00000002 million dollars worth,
              --- Shawn

              "Probability is now one. Any problems that are left are your own."
              SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

              * Perl tutorials at http://perlmonks.org/?node=Tutorials
              * A searchable perldoc is available at http://perldoc.perl.org/
            • KalyanRaj
              Hi, Mail Meda - the following code will give you what you want....PBML. another version of code which uses hash and return sub reference { $type = shift;
              Message 6 of 20 , Jan 2, 2006
              • 0 Attachment
                Hi,

                Mail Meda - the following code will give you what you want....PBML.
                another version of code which uses hash and return
                sub reference {
                $type = shift;
                %types = (
                group => "PBML",
                lang => "Perl",
                os => "Linux"
                );
                return $types{$type};
                }
                $result = reference("group");
                print "$result\n";

                Cheers,
                KalyanRaj

                -----Original Message-----
                From: perl-beginner@yahoogroups.com
                [mailto:perl-beginner@yahoogroups.com]On Behalf Of Shawn Corey
                Sent: Monday, January 02, 2006 11:28 PM
                To: perl-beginner@yahoogroups.com
                Subject: Re: [PBML] Variable Interpolation


                Randal L. Schwartz wrote:
                >>>>>>"Shawn" == Shawn Corey <shawn.corey@...> writes:
                >
                >
                > Shawn> You should always use an argument for shift.
                >
                > That's a bit strong. It's accepted to leave the argument off shift
                > when it's being used in a sane way. I presumed the original poster
                > knew what he was doing, and didn't comment on that.
                >

                Yes, it's strong but all good discipline seems strong until you reap the
                benefits from it. I don't think it's sane for the implied behaviour of
                any construct to change. What it means is that when I'm reading a
                program, I have one more thing to keep track of. Anything that reduces
                this overhead is worth the effort.

                Always write your programs as though you aren't going to see them again
                for 25 years and then have to make a critical change immediately.


                --

                Just my 0.00000002 million dollars worth,
                --- Shawn

                "Probability is now one. Any problems that are left are your own."
                SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

                * Perl tutorials at http://perlmonks.org/?node=Tutorials
                * A searchable perldoc is available at http://perldoc.perl.org/


                Unsubscribing info is here:
                http://help.yahoo.com/help/us/groups/groups-32.html
                Yahoo! Groups Links
              • Jenda Krynicky
                From: Shawn Corey ... I would be a bit surprised if anyone wrote: sub foo { my $bar = shift(@_); ... and would wonder why. In this
                Message 7 of 20 , Jan 3, 2006
                • 0 Attachment
                  From: Shawn Corey <shawn.corey@...>
                  > Randal L. Schwartz wrote:
                  > >>>>>>"Shawn" == Shawn Corey <shawn.corey@...> writes:
                  > >
                  > >
                  > > Shawn> You should always use an argument for shift.
                  > >
                  > > That's a bit strong. It's accepted to leave the argument off shift
                  > > when it's being used in a sane way. I presumed the original poster
                  > > knew what he was doing, and didn't comment on that.
                  > >
                  >
                  > Yes, it's strong but all good discipline seems strong until you reap
                  > the benefits from it. I don't think it's sane for the implied
                  > behaviour of any construct to change. What it means is that when I'm
                  > reading a program, I have one more thing to keep track of. Anything
                  > that reduces this overhead is worth the effort.
                  >
                  > Always write your programs as though you aren't going to see them
                  > again for 25 years and then have to make a critical change
                  > immediately.

                  I would be a bit surprised if anyone wrote:

                  sub foo {
                  my $bar = shift(@_);
                  ...

                  and would wonder why. In this case it's a generally accepted that the
                  @_ gets omited. OTOH, I'd never make use of the fact that outside
                  subroutines shift() means shift(@ARGV). That's something lots of
                  people doesn't remember and something that might break easily if I
                  tried to move the piece of code into a subroutine.

                  Jenda
                  ===== Jenda@... === http://Jenda.Krynicky.cz =====
                  When it comes to wine, women and song, wizards are allowed
                  to get drunk and croon as much as they like.
                  -- Terry Pratchett in Sourcery
                • merlyn@stonehenge.com
                  ... Jenda I would be a bit surprised if anyone wrote: Jenda sub foo { Jenda my $bar = shift(@_); Jenda ... Jenda and would wonder why. In this case
                  Message 8 of 20 , Jan 3, 2006
                  • 0 Attachment
                    >>>>> "Jenda" == Jenda Krynicky <Jenda@...> writes:

                    Jenda> I would be a bit surprised if anyone wrote:

                    Jenda> sub foo {
                    Jenda> my $bar = shift(@_);
                    Jenda> ...

                    Jenda> and would wonder why. In this case it's a generally accepted that the
                    Jenda> @_ gets omited. OTOH, I'd never make use of the fact that outside
                    Jenda> subroutines shift() means shift(@ARGV). That's something lots of
                    Jenda> people doesn't remember and something that might break easily if I
                    Jenda> tried to move the piece of code into a subroutine.

                    I agree with you there. However, for a five line program:

                    #!/usr/bin/perl
                    my $from = shift;
                    my $to = shift;
                    if (-e $to) { rename $to, "$to~" or warn "cannot backup $to: $!"; }
                    rename $from, $to or warn "Cannot rename $from to $to: $!";

                    Will anyone really not know what "shift" is doing there? Or confuse
                    it with shift(@_)?

                    That's the point of defaults... they're the defaults because they're
                    the most common thing you would do with that operator *in that context*.

                    --
                    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                    <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
                    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
                  • Shawn Corey
                    ... The key phrase being in that context . My issue is that the default depends on the context. If it was the same throughout the regardless of context, I
                    Message 9 of 20 , Jan 3, 2006
                    • 0 Attachment
                      merlyn@... wrote:
                      > That's the point of defaults... they're the defaults because they're
                      > the most common thing you would do with that operator *in that context*.
                      >

                      The key phrase being "in that context". My issue is that the default
                      depends on the context. If it was the same throughout the regardless of
                      context, I wouldn't have a problem. The problem is that when I see a
                      naked shift, I have to stop and remember the context. One more thing to
                      worry about while reading a program.


                      --

                      Just my 0.00000002 million dollars worth,
                      --- Shawn

                      "Probability is now one. Any problems that are left are your own."
                      SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

                      * Perl tutorials at http://perlmonks.org/?node=Tutorials
                      * A searchable perldoc is available at http://perldoc.perl.org/
                    • merlyn@stonehenge.com
                      ... Shawn The key phrase being in that context . My issue is that the default Shawn depends on the context. If it was the same throughout the regardless of
                      Message 10 of 20 , Jan 3, 2006
                      • 0 Attachment
                        >>>>> "Shawn" == Shawn Corey <shawn.corey@...> writes:

                        Shawn> The key phrase being "in that context". My issue is that the default
                        Shawn> depends on the context. If it was the same throughout the regardless of
                        Shawn> context, I wouldn't have a problem. The problem is that when I see a
                        Shawn> naked shift, I have to stop and remember the context. One more thing to
                        Shawn> worry about while reading a program.

                        That's why I said "10 line program". If this were a 200 line program,
                        I wouldn't bury "shift" in the middle, unless it was within 10 lines
                        of the start of a subroutine.

                        I believe it is reasonable to ask a programmer to look back about
                        ten lines to figure out the general flow of the program. I don't
                        think it's reasonable to have to maintain "state" further than that,
                        which is why I argue for 10-20 line subroutines instead of most of
                        the monoliths that I see most people write.

                        --
                        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                        Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
                        See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
                      • Shawn Corey
                        ... Large monoliths have given me the willies ever since 2001: A Space Odyssey came out ;) -- Just my 0.00000002 million dollars worth, ... Probability is
                        Message 11 of 20 , Jan 3, 2006
                        • 0 Attachment
                          Randal L. Schwartz wrote:
                          > I believe it is reasonable to ask a programmer to look back about
                          > ten lines to figure out the general flow of the program. I don't
                          > think it's reasonable to have to maintain "state" further than that,
                          > which is why I argue for 10-20 line subroutines instead of most of
                          > the monoliths that I see most people write.
                          >

                          Large monoliths have given me the willies ever since "2001: A Space
                          Odyssey" came out ;)


                          --

                          Just my 0.00000002 million dollars worth,
                          --- Shawn

                          "Probability is now one. Any problems that are left are your own."
                          SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

                          * Perl tutorials at http://perlmonks.org/?node=Tutorials
                          * A searchable perldoc is available at http://perldoc.perl.org/
                        • mail meda
                          Hi Frnds, Thanks for your response. Bye. ... [Non-text portions of this message have been removed]
                          Message 12 of 20 , Jan 5, 2006
                          • 0 Attachment
                            Hi Frnds,

                            Thanks for your response.

                            Bye.


                            On 1/4/06, Shawn Corey <shawn.corey@...> wrote:
                            >
                            > Randal L. Schwartz wrote:
                            > > I believe it is reasonable to ask a programmer to look back about
                            > > ten lines to figure out the general flow of the program. I don't
                            > > think it's reasonable to have to maintain "state" further than that,
                            > > which is why I argue for 10-20 line subroutines instead of most of
                            > > the monoliths that I see most people write.
                            > >
                            >
                            > Large monoliths have given me the willies ever since "2001: A Space
                            > Odyssey" came out ;)
                            >
                            >
                            > --
                            >
                            > Just my 0.00000002 million dollars worth,
                            > --- Shawn
                            >
                            > "Probability is now one. Any problems that are left are your own."
                            > SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_
                            >
                            > * Perl tutorials at http://perlmonks.org/?node=Tutorials
                            > * A searchable perldoc is available at http://perldoc.perl.org/
                            >
                            >
                            > Unsubscribing info is here:
                            > http://help.yahoo.com/help/us/groups/groups-32.html
                            > Yahoo! Groups Links
                            >
                            >
                            >
                            >
                            >
                            >
                            >
                            >


                            [Non-text portions of this message have been removed]
                          Your message has been successfully submitted and would be delivered to recipients shortly.