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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

output for formula

Status
Not open for further replies.

oysters2000

Programmer
Joined
Feb 14, 2005
Messages
5
Location
GB

Hi expert
I will be writing my exam next week and need some explaining on the following:

1. I need help with the following code, if possible. could you please explain how they work out the 2nd print of the string, and what does $1 stand for. I have the answer but just checking if there are other solutions.
$str ="A fox in a box trading socks";
$str =~m/..(...)../;
print ("$1 ");
$str =~m/\s+$1.in\sa\s[a-d]{1}(\w{2}); # this part i don’t understand(how do they get “ox”)
print "$1 ";
$str =~m/([stock]{6});
print "$1 ";

2. in the pre-assements of the hash’s, the second question I don’t understand how they get their answer of 52?
%hash = (1=>2
2 =>3
3 =>4);

$hash{2} = 5;
$hash = 6;
%hash = reverse(%hash);

for ($i = 1; $i < 5; $i++)
{
delete($hash{$1});
}
print(%hash)


If you could get back to I would appreciate it.

Regards mark

 
Code:
$str =~m/\s+$1.in\sa\s[a-d]{1}(\w{2})/;
That regexp is matching against the original string in $str. The $1 variable currently contains 'fox' (from the first regexp). Here it is broken down:
\s+ (one or more spaces, which matches the space after 'A')
$1 (matches 'fox')
. (any character - matches the space after 'fox')
in\s (matches 'in', followed by the space aftewards)
a\s (matches 'a', followed by the space afterwards)
[a-d]{1} (one character that is a,b,c or d - matches the 'b' in 'box')
(\w{2}) (exactly 2 'word characters' (i.e. a-z, 0-9 or _) - matches the 'ox' in 'box')

Because the \w{2} is in parenthesis, whatever it matches ('ox') is stored in $1.


Second one explained:
Code:
# set up the initial hash - easy!
%hash = (1=>2,
2 =>3,
3 =>4);

# set the value of key '2' to 5.
# %hash is now 1=>2, 2=>5, 3=>4
$hash{2} = 5;

# this is a scalar unrelated to %hash
$hash = 6;

# reverse the hash - it's now 4=>3, 5=>2, 2=>1
%hash = reverse(%hash);

# delete any elements which have a key of 1, 2, 3 or 4
# hash is now 5=>2
for ($i = 1; $i < 5; $i++)
{
delete($hash{$i}); # assuming '1' should have been 'i'
}

# print 52 (<key><value>)
print(%hash);
 
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);
 
oysters2000, Please don't double-post the same question in different threads.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top