1. Registered User
Join Date
Feb 2005
Posts
1

i need help with the following code, if possible. could someone 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 (they get ox)
print "\$1 ";

\$str =~m/([stock]{6});
print "\$1 ";

and the 2nd one please, do you know what the output is?.

%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)

2. Registered User
Join Date
Jun 2004
Location
Nowhere Near You
Posts
89
Second question ---

Code:
%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)
one gets some pretty nasty remarks from perl since there are missing ,'s. It should read something like

%hash=(1=>2,2=>3,...);

moreover that "delete(\$hash{\$1})" probably should be "delete(\$hash{\$i})". Maybe that what's wrong?

Learn to be lazy --- Use "use strict;" "use warnings;" as in
Code:
#!/usr/bin/perl

use strict;
use warnings;

my %hash = (1=>2
,2 =>3
,3 =>4);

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

for (my(\$i) = 1; \$i < 5; \$i++)
{
delete(\$hash{\$1});
};
print(%hash)
Last edited by senza_nome; 02-10-05 at 21:14.

3. Registered User
Join Date
Jan 2004
Location
Germany
Posts
167
Code:
\$str =~m/\s+         # one or more whitespaces
\$1         # from the first regex (the first "cache"d result)
.           # any "sign", letter, digit or what ever
in          # the word "in" (it's literal)
\s          # one whitespace
a           # the letter "a"
\s          # one whitespace
[a-d]{1}  # one letter (a, b, c or d)
(\w{2})   # two alphanumerical signs and save these two signs in \$1
/x;
\$1, \$2 ... are explained in perldoc perlvar!

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•