Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

output results

Status
Not open for further replies.

oysters2000

Programmer
Feb 14, 2005
5
GB
I don’t understand how they get their answer as the code is not that clear.
Could some one could explain line by line how they get their answer, I would really appreciate it.




sub returnValues {
my($target, @values) = @_;
my(@results);
foreach(@values) {
$_ > $target ? push(@results, “$_\n”) : “”;
}
return @results;
}

using this subroutine, what is the output of the following perl statements?

@myNumList = (33, 35, 67, 11, 78, 99, 23, 55);
print returnValues(50, @myNumList);
 
Here's a quick and dirty explanation. Like ishnid said, which bit don't you understand?

I rewrote the '$_ ? stuff : ""' statement for clarity. Essentially all it does is checks whether the current $_ is greater than $target and, if yes, (does whatever is after the [red]?[/red]) adds it to the @results, if not (and this part is unnecessary because it does nothing - but it's the code after the [red]:[/red]) does nothing. (In fact you'll get a warning similar to "useless use of constant in void context." if you run the code you posted.)

Code:
sub returnValues {
    my($target, @values) = @_;  # Declares $target and @values. Copies first
                                # argument into $target, remaining go to @values
    my(@results);               # Declares @results
    foreach(@values) {          # foreach item in @values (assigned to $_)
        #$_ > $target ? push(@results, "$_\n") : "";    # Rewritten below
        if ($_ > $target) {
            push(@results, "$_\n"); # Adds current value of $_ and a new line
                                    # character to @results
        }
    }
    return @results;            # Returns contents of @results;
}

my @myNumList = (33, 35, 67, 11, 78, 99, 23, 55);   # Creates @myNumList and
                                                    # populates it with values
print returnValues(50, @myNumList);                 # prints results of &returnValues
 
rharsh's explanation is fine, but the wrapping of the commented line-ends make it a little hard to read, for me at least. Thought you might find the following a little more readable:
Code:
[b]Code:[/b]
 1. sub returnValues {
 2.     my($target, @values) = @_;
 3.     my(@results);
 4.     foreach(@values) {
 5.         if ($_ > $target) {
 6.             push(@results, "$_\n");
 7.         }
 8.     }
 9.     return @results;
10. }
11. my @myNumList = (33, 35, 67, 11, 78, 99, 23, 55);
12. print returnValues(50, @myNumList);

[b]Comments:[/b]
 2. Declares $target and @values. Copies first argument
    into $target, remaining go to @values
 3. Declares @results
 4. foreach item in @values (assigned to $_)
 6. Adds current value of $_ and a new line character to
    @results
 9. Returns contents of @results;
11. Creates @myNumList and populates it with values
12. prints results of &returnValues
I produced the above output from rharsh's post with the following script. (Okay, I did edit a little bit.):
Code:
#!perl
use strict;
use warnings;

my (@code, @comments);
my $lineno = 0;
my $cstr = '';

while (<>) {
   chomp;
   /^\s*$/ && next;
   my ($code, $comment) = map {s/\s+$//; $_} split /#\s*/;
   if ($code) {
       if ($cstr) {
           push(@comments, [$lineno, $cstr]);
           $cstr = '';
       }
       $lineno++;
       push(@code, [$lineno, $code]);
   } 
   if ($comment) {
       $cstr .= $cstr? ' ' . $comment: $comment;
   }
}
push(@comments, [$lineno, $cstr]) if $cstr;

my $str = "Code";
for (\@code, \@comments) {
    printem($str, $_);
    print "\n";
    $str = "Comments"
}

sub printem {
    my ($str, $arr) = @_;
    print "$str:\n";
    printf "%2d. %s\n", @$_ for @$arr;
}
rharsh, thanks for pointing out that
$_ > $target ? push(@results, “$_\n”) : “”;
is a terrible way to write that. :)

HTH



 
I think this is a little bit too much like asking us to do your homework for you. Why don't you tell us what you think the answers are?
 
OP posted same question in this thread: thread219-1007681.
Don't waste your time with this one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top