Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 2nd, 2012, 02:06 PM
lie_soul lie_soul is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 7 lie_soul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 17 m 17 sec
Reputation 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:

Quote:
#!/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 !!!

Reply With Quote
  #2  
Old October 2nd, 2012, 04:28 PM
Laurent_R Laurent_R is online now
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 502 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 18 h 51 m 13 sec
Reputation Power: 385
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?

Reply With Quote
  #3  
Old October 2nd, 2012, 04:32 PM
lie_soul lie_soul is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 7 lie_soul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 17 m 17 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #4  
Old October 3rd, 2012, 03:21 AM
Laurent_R Laurent_R is online now
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 502 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 18 h 51 m 13 sec
Reputation Power: 385
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.

Reply With Quote
  #5  
Old October 3rd, 2012, 03:57 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 14 m 53 sec
Reputation Power: 3885
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.

Reply With Quote
  #6  
Old October 3rd, 2012, 08:40 AM
lie_soul lie_soul is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 7 lie_soul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 17 m 17 sec
Reputation Power: 0
Quote:
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..

Reply With Quote
  #7  
Old October 3rd, 2012, 08:49 AM
lie_soul lie_soul is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 7 lie_soul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 17 m 17 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #8  
Old October 3rd, 2012, 10:12 AM
Laurent_R Laurent_R is online now
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 502 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 18 h 51 m 13 sec
Reputation Power: 385
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:
Original - Perl Code
  1.  
  2. #!/usr/bin/perl
  3. use strict;
  4. use warnings;
  5.  
  6. my @values = ('z', 'y' 'r', '20', '30');
  7.  
  8. print "Original outside: @values\n";
  9.  
  10. func( \@values);
  11.  
  12. my @new_values = (@values [0..2], 'something else', @values [3..4]);
  13. # Now, @new_values contains:  ('z', 'y' 'r', 'something else', '20', '30');
  14.  
  15. print "New values are: @new_values";
  16.  
  17. sub func
  18. {
  19.      my $ref_values = $_[0];
  20.      print "Inside sub: $$ref_values\n";
  21.  
  22. } 


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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > My code output ans sample output looks different..

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap