#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    My code output ans sample output looks different..


    Hello,

    this is the sample output:

    Original outside: z y z 20 30
    Passed inside sub: z y z 20 30
    Changed and printed outside: z y another_thing 20 30

    this is my code and output:

    #!/usr/bin/perl
    use strict;
    use warnings;
    my @values = ("z", "y", "r", "20", "30");
    print "Original outside: @values\n";

    sub func { $ref_values = @_[0];
    print "Passed inside sub: @$ref_values\n";
    }
    &func( \@values);
    Output:
    Original outside: z y r 20 30
    Passed inside sub: z y r 20 30

    i can't come up with the third output and figure out a way to change the index 2 to print something else !!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    Hmm, if I understand what you want, you are expecting a third line of output, but there is no instruction to print a third line in your code, it won't magically print if you don't ask for it.

    Or did I miss something from your problem?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by Laurent_R
    Hmm, if I understand what you want, you are expecting a third line of output, but there is no instruction to print a third line in your code, it won't magically print if you don't ask for it.

    Or did I miss something from your problem?
    i need help for the third line.. tried but couldn't come up with a way to help me solve it.. i need to figure out a third way to print it outside the sub with changing the second index. so i can get the same sample output:

    Changed and printed outside: z y [another_thing] 20 30

    [another_thing] = index 2 which is z

    so my final output would be:

    Original outside: z y z 20 30
    Passed inside sub: z y z 20 30
    Changed and printed outside: z y another_thing 20 30
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    Your func subroutine does not modify the array, so if you print the array after the call to the subroutine, you'll get the same output as before the call.

    If you need to add an element to the array, this is possible, but please tell us where you want to do it.
  8. #5
  9. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    That code won't run at all! You've included "use strict;" but you haven't declared your $ref_values variable. You also have some quite old-fashioned and ill-advised practices in there. Here's how I'd write that exact program (I've also included hints on where you'll need to write code to solve your problem):

    Code:
    #!/usr/bin/perl 
    use strict; 
    use warnings;
    
    # no need for double quotes for simple strings
    # you don't require interpolation
    my @values = ( 'z', 'y' 'r', '20', '30' );
    
    # you need @values to be interpolated, so double quotes fit here
    print "Original outside: @values\n"; 
    
    # keep this here: putting the definition of a subroutine in 
    # the middle of your code makes it very hard to read
    # Also, don't use & for calling subroutines unless you have a good reason too do so.
    func( \@values );
    
    # HINT: you need to print the values in the array again here
    
    sub func {
       # you'd forgotten to declare $ref_values
       # Also, use $, not @ for referring to a single element
       # in an array.
       # the following line could also be written:
       #   my $ref_values = shift;
       my $ref_values = $_[0];
    
       print "Passed inside sub: @$ref_values\n";
    
       # HINT: you need to change the contents of the array here
    }
    Last edited by ishnid; October 3rd, 2012 at 04:04 AM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by Laurent_R
    Your func subroutine does not modify the array, so if you print the array after the call to the subroutine, you'll get the same output as before the call.

    If you need to add an element to the array, this is possible, but please tell us where you want to do it.
    i want to print the array with chaining index 2 to anything i want outside the sub..

    that what i'm having problem with..
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by ishnid
    That code won't run at all! You've included "use strict;" but you haven't declared your $ref_values variable. You also have some quite old-fashioned and ill-advised practices in there. Here's how I'd write that exact program (I've also included hints on where you'll need to write code to solve your problem):

    Code:
    #!/usr/bin/perl 
    use strict; 
    use warnings;
    
    # no need for double quotes for simple strings
    # you don't require interpolation
    my @values = ( 'z', 'y' 'r', '20', '30' );
    
    # you need @values to be interpolated, so double quotes fit here
    print "Original outside: @values\n"; 
    
    # keep this here: putting the definition of a subroutine in 
    # the middle of your code makes it very hard to read
    # Also, don't use & for calling subroutines unless you have a good reason too do so.
    func( \@values );
    
    # HINT: you need to print the values in the array again here
    
    sub func {
       # you'd forgotten to declare $ref_values
       # Also, use $, not @ for referring to a single element
       # in an array.
       # the following line could also be written:
       #   my $ref_values = shift;
       my $ref_values = $_[0];
    
       print "Passed inside sub: @$ref_values\n";
    
       # HINT: you need to change the contents of the array here
    }

    i modify it...

    Code:
    #!/usr/bin/perl 
    use strict; use 
    warnings; my 
    
    @values = ('z', 'y' 'r', '20', '30');
    
    print "Original outside: @values\n"; 
    
    sub func 
    { 
    
    $ref_values = $_[0];
    print "Inside sub: $$ref_values\n";
    
    } 
    &func( \@values);
    
    print "@values";
    Now, where do i add the third request to print the values with changing the the third index? to look like this output;

    Original outside: z y z 20 30
    Passed inside sub: z y z 20 30
    Changed and printed outside: z y another_thing 20 30
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    Hi,

    as Ishnid said, don't put your function definition in the middle of the code, put it either at the beginning, or probably better at the end. You also don't need "&" to call your function.

    I still haven't fully understood what you want to do, so I'll give a try to see if it matches what you want to do. This is untested, as I am traveling and have currectly no access to a Perl environment.

    Perl Code:
     
    #!/usr/bin/perl 
    use strict;
    use warnings; 
     
    my @values = ('z', 'y' 'r', '20', '30');
     
    print "Original outside: @values\n"; 
     
    func( \@values);
     
    my @new_values = (@values [0..2], 'something else', @values [3..4]);
    # Now, @new_values contains:  ('z', 'y' 'r', 'something else', '20', '30');
     
    print "New values are: @new_values";
     
    sub func 
    { 
         my $ref_values = $_[0];
         print "Inside sub: $$ref_values\n";
     
    }


    Note that I have chosen to simply create a new array and copy the elements, you could modify the existing array as well. Also note that there are other ways to add fields to an array (or remove from an array), such as the splice function. Look for the splice function in the Perl documentation.

IMN logo majestic logo threadwatch logo seochat tools logo