If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > need help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-10-05, 09:05
oysters2000 oysters2000 is offline
Registered User
 
Join Date: Feb 2005
Posts: 1
need help

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)
Reply With Quote
  #2 (permalink)  
Old 02-10-05, 20:09
senza_nome senza_nome is offline
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 20:14.
Reply With Quote
  #3 (permalink)  
Old 02-11-05, 03:45
reneeb reneeb is offline
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!
__________________
board.perl-community.de - The German Perl-Community
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On