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

reuse of func result

Expand Messages
  • John Lucania
    I am trying to get the second function (random_chars_2nd) working. random_chars_2nd should return the random characters generated in the 1st function
    Message 1 of 4 , Oct 11, 2005
      I am trying to get the second function (random_chars_2nd) working.
      random_chars_2nd should return the random characters generated in the
      1st function (random_chars).

      How do you do that?

      #! /usr/bin/perl
      use strict;
      use warnings;

      my $WhichLength;

      print "enter random length.\n";

      $WhichLength =<STDIN>;

      sub random_chars {
      my @charlist = ( 'A' , 'B' , 'C', 'D' );
      my $CombinedSeq;

      foreach ( 1 .. $WhichLength ) {
      my $random_element = $charlist[ rand( @charlist ) ];
      print $random_element;
      }
      }

      print random_chars ($WhichLength);

      sub random_chars_2nd {

      foreach ( 1 .. $WhichLength ) {
      my $random_element = $random_element[ rand(
      $random_element ) ]; ???????
      print $random_element;
      }
      }

      print random_chars_2nd ($WhichLength);
    • Jeff 'japhy' Pinyan
      ... What you want is a function that creates a string of random characters, not one that prints out the characters. The i/o should be handled at the top-most
      Message 2 of 4 , Oct 11, 2005
        On Oct 11, John Lucania said:

        > I am trying to get the second function (random_chars_2nd) working.
        > random_chars_2nd should return the random characters generated in the
        > 1st function (random_chars).

        What you want is a function that creates a string of random characters,
        not one that prints out the characters. The i/o should be handled at the
        top-most level; random_chars() should just produce the string.

        > How do you do that?

        By adding the characters to a string instead of printing them.

        > sub random_chars {
        > my @charlist = ( 'A' , 'B' , 'C', 'D' );
        > my $CombinedSeq;
        >
        > foreach ( 1 .. $WhichLength ) {
        > my $random_element = $charlist[ rand( @charlist ) ];
        > print $random_element;
        > }
        > }

        Try this:

        # random_chars($number, @char_set)

        sub random_chars {
        my $count = shift; # get the number of chars to produce
        my $string;

        while ($count--) {
        $string .= $_[rand @_];
        }

        return $string;
        }

        print random_chars(5, "A" .. "E"); # "AEDDC", perhaps

        Try that. Incidentally:

        > print random_chars ($WhichLength);
        > print random_chars_2nd ($WhichLength);

        You passed $WhichLength to the functions, but you never GOT the value from
        @_, you just used $WhichLength in the function. That's not how argument
        passing works.

        --
        Jeff "japhy" Pinyan % How can we ever be the sold short or
        RPI Acacia Brother #734 % the cheated, we who for every service
        http://www.perlmonks.org/ % have long ago been overpaid?
        http://princeton.pm.org/ % -- Meister Eckhart
      • Shawn Corey
        ... I m not exactly sure what you want to do. Are these modifications close? ... my @Random_Elements = (); ... # $random_element is only scoped within the
        Message 3 of 4 , Oct 11, 2005
          John Lucania wrote:
          > I am trying to get the second function (random_chars_2nd) working.
          > random_chars_2nd should return the random characters generated in the
          > 1st function (random_chars).
          >
          > How do you do that?

          I'm not exactly sure what you want to do. Are these modifications close?

          >
          > #! /usr/bin/perl
          > use strict;
          > use warnings;
          >
          > my $WhichLength;
          my @Random_Elements = ();
          >
          > print "enter random length.\n";
          >
          > $WhichLength =<STDIN>;
          >
          > sub random_chars {
          > my @charlist = ( 'A' , 'B' , 'C', 'D' );
          > my $CombinedSeq;
          >
          > foreach ( 1 .. $WhichLength ) {
          > my $random_element = $charlist[ rand( @charlist ) ];
          # $random_element is only scoped within the foreach block
          > print $random_element;
          push @Random_Elements, $random_element;
          > }
          # From here on any reference to $random_element will cause a syntax error.
          > }
          >
          > print random_chars ($WhichLength);
          >
          > sub random_chars_2nd {
          >
          > foreach ( 1 .. $WhichLength ) {
          > my $random_element = $random_element[ rand(
          > $random_element ) ]; ???????
          my $random_element = $Random_Elements[ rand(
          @Random_Elements ) ] ;
          # Note that this $random_element is less random than the previous one.
          > print $random_element;
          > }
          > }
          >
          > print random_chars_2nd ($WhichLength);
          >
        • John Lucania
          Yes, it helps a lot. I got it now.
          Message 4 of 4 , Oct 11, 2005
            Yes, it helps a lot.
            I got it now.

            On 10/11/05, Shawn Corey <shawn.corey@...> wrote:
            > John Lucania wrote:
            > > I am trying to get the second function (random_chars_2nd) working.
            > > random_chars_2nd should return the random characters generated in the
            > > 1st function (random_chars).
            > >
            > > How do you do that?
            >
            > I'm not exactly sure what you want to do. Are these modifications close?
            >
            > >
            > > #! /usr/bin/perl
            > > use strict;
            > > use warnings;
            > >
            > > my $WhichLength;
            > my @Random_Elements = ();
            > >
            > > print "enter random length.\n";
            > >
            > > $WhichLength =<STDIN>;
            > >
            > > sub random_chars {
            > > my @charlist = ( 'A' , 'B' , 'C', 'D' );
            > > my $CombinedSeq;
            > >
            > > foreach ( 1 .. $WhichLength ) {
            > > my $random_element = $charlist[ rand( @charlist ) ];
            > # $random_element is only scoped within the foreach block
            > > print $random_element;
            > push @Random_Elements, $random_element;
            > > }
            > # From here on any reference to $random_element will cause a syntax error.
            > > }
            > >
            > > print random_chars ($WhichLength);
            > >
            > > sub random_chars_2nd {
            > >
            > > foreach ( 1 .. $WhichLength ) {
            > > my $random_element = $random_element[ rand(
            > > $random_element ) ]; ???????
            > my $random_element = $Random_Elements[ rand(
            > @Random_Elements ) ] ;
            > # Note that this $random_element is less random than the previous one.
            > > print $random_element;
            > > }
            > > }
            > >
            > > print random_chars_2nd ($WhichLength);
            > >
            >
            >
            >
            > Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html
            > Yahoo! Groups Links
            >
            >
            >
            >
            >
            >
            >
            >
            >
          Your message has been successfully submitted and would be delivered to recipients shortly.