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

"Alias" for a Variable?

Expand Messages
  • Rick
    Hello, Another newbie question: Given the following lines of code: -- #!/usr/bin/perl print( Enter A - F , One Line at a Time or -99 to Quit: n );
    Message 1 of 3 , Nov 3, 2002
    • 0 Attachment
      Hello,
      Another newbie question:
      Given the following lines of code:
      --
      #!/usr/bin/perl
      print("Enter \"A - F\", One Line at a Time or \"-99\" to Quit: \n");
      chomp(@x = <STDIN>);
      print ("The Array is: @x \n\n");
      $shiftx = shift @x;
      $popx = pop @x;
      $sizex = @x;
      $swapx = (($x[1],$x[2]) = ($x[2],$x[1]));
      @R = reverse @x;
      print ("First removed: $shiftx \n\n");
      print ("Last removed: $popx \n\n");
      print ("Size: $sizex \n\n");
      print ("X[0]: @x[0] \n\n") ;
      print ("X[1]: @x[1] \n\n");
      print ("X[2]: @x[2] \n\n");
      print ("X[3]: @x[3] \n\n\n");
      print ("R[0]: @R[0] \n\n");
      print ("R[1]: @R[1] \n\n");
      print ("R[2]: @R[2] \n\n");
      print ("R[3]: @R[3] \n\n");
      #END
      --
      I want to have my "Quit" statememt = -99 (or whatever) via assigning
      it the \cD escape (which, to my understanding, equates to <CTRL-D>.
      However, my limited Perl knowledge tells me I would first have to
      assign a variable to = \cD ala:
      $endIt = ("\cD");
      But then, how would I place it within my script so that the enterer
      could enter -99 and have Perl recognize this as the signal to Quit?
      See my dilemma? I want $endIt to = \cD AND -99
      also, would chr(4) which is EOT mean the same thing as <CTrL-D>
      I know I could command a QUIT by stating something like: While (@x
      ne -99) but I wanted to try something trickier :)

      Sorry this is sooo long-winded, but any suggestions would be great :)

      r.
    • Jeff 'japhy' Pinyan
      ... If the user presses Ctrl-D, STDIN will stop reading. However, you want -99 to have the same effect. while ( ) { chomp; last if $_ eq -99 ; # or
      Message 2 of 3 , Nov 3, 2002
      • 0 Attachment
        On Nov 4, Rick said:

        >print("Enter \"A - F\", One Line at a Time or \"-99\" to Quit: \n");
        >chomp(@x = <STDIN>);

        If the user presses Ctrl-D, STDIN will stop reading. However, you want
        -99 to have the same effect.

        while (<STDIN>) {
        chomp;
        last if $_ eq '-99'; # or whatever value you want
        push @x, $_;
        }

        That will work in place of

        chomp(@x = <STDIN>);

        >$swapx = (($x[1],$x[2]) = ($x[2],$x[1]));

        Why are you storing the value? And your swap is better written as

        @x[1,2] = @x[2,1];

        >print ("X[0]: @x[0] \n\n") ;

        Why are you using @x[0] here, instead of $x[1]? Your code should have
        warnings on (via the -w switch).

        --
        Jeff "japhy" Pinyan japhy@... http://www.pobox.com/~japhy/
        RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
        <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
        [ I'm looking for programming work. If you like my work, let me know. ]
      • Rick
        ... Because Perl is new to me, and i didn t know better. ... Of course...thanks. ... Because Perl is new to me, and i didn t know better. ... Ok, I put the -w
        Message 3 of 3 , Nov 5, 2002
        • 0 Attachment
          > >$swapx = (($x[1],$x[2]) = ($x[2],$x[1]));
          >
          > Why are you storing the value?

          Because Perl is new to me, and i didn't know better.


          > And your swap is better written as
          >
          > @x[1,2] = @x[2,1];

          Of course...thanks.

          >
          > >print ("X[0]: @x[0] \n\n") ;
          >
          > Why are you using @x[0] here, instead of $x[1]?

          Because Perl is new to me, and i didn't know better.


          > Your code should have
          > warnings on (via the -w switch).

          Ok, I put the -w switch on and the warnings did indeed surface.
          Strange how it works, but It shouldn't have. I heard that Perl makes
          a lot of assumptions....sometimes its for the good, but could back-
          fire.

          thanks for your help :)

          rs

          >
          > --
          > Jeff "japhy" Pinyan japhy@p...
          http://www.pobox.com/~japhy/
          > RPI Acacia brother #734 http://www.perlmonks.org/
          http://www.cpan.org/
          > <stu> what does y/// stand for? <tenderpuss> why, yansliterate of
          course.
          > [ I'm looking for programming work. If you like my work, let me
          know. ]
        Your message has been successfully submitted and would be delivered to recipients shortly.