scope of the variable?

2006-02-02 Thread bogum
Hello All,
I have following question regarding accessing variable from other module:

In test.pm I have following:

#BEGIN OF THE TEST.PM
package test;

use strict;
use warnings;

#
# The object responsible for managing the database connections.
#
my $dbaccess = undef;

-somewhere else 
$dbaccess = new ::xxx::DBAccess( %dbURL);

….
….

#END OF THE TEST.PM

In test2.pm I have following:

#BEGIN OF THE TEST2.PM
package test2;

use strict;
use warnings;

….
#  How to test::dbaccess ??

#END OF THE TEST2.PM


My question is how to access $dbaccess variable (object) defined and 
initialized in test.pm within test2.pm module?

Thanks,
Bogumil

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: extracting information from a file

2006-02-02 Thread Dhanashri Bhate
Hi,
Give the sample data and then only one can tell what kind of regex you need.
If your data is simple, just fields delimited by ">" you don't need to write
regex, you can just work with "split". Read the data lines one by one, and
for each line use split to get the fields in an array.
Also remember the list is to help perl beginners, so no one will respond
positively if you just ask, I want to do this and help me. 
Read Perl docs, try something yourself and then post if you face any
problems.

Regards,
Dhanashri


-Original Message-
From: Rana [mailto:[EMAIL PROTECTED] 
Sent: Friday, February 03, 2006 8:24 AM
To: beginners@perl.org
Subject: extracting information from a file

Hi,
I am a new bee to perl and I am trying to extract data present between
delimiter ">". I am not exactly sure how to write the regular expression so
that I can assign the extracted information to a string or an array.

Kindly help me in this regard.

Thank you.
Rana


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




extracting information from a file

2006-02-02 Thread Rana
Hi,
I am a new bee to perl and I am trying to extract data present between
delimiter ">". I am not exactly sure how to write the regular expression so
that I can assign the extracted information to a string or an array.

Kindly help me in this regard.

Thank you.
Rana


about the TIME_WAIT

2006-02-02 Thread Sophie Joe

hi,here,
When the socket clients connect to the server,there are 
many TIME_WAIT messages appearing in clients's tcp 
connection status as following:

$ netstat -an
tcp0  0 192.168.0.130:50124 
 192.168.0.10:31330TIME_WAIT
tcp0  0 192.168.0.130:50125 
 192.168.0.10:31330TIME_WAIT
tcp0  0 192.168.0.130:50127 
 192.168.0.10:31330TIME_WAIT
tcp0  0 192.168.0.130:50120 
 192.168.0.10:31330TIME_WAIT
tcp0  0 192.168.0.130:50121 
 192.168.0.10:31330TIME_WAIT
tcp0  0 192.168.0.130:50122 
 192.168.0.10:31330TIME_WAIT

The clients and server are all written in Perl language.
For what reasons to produce this circs and how to 
eliminate it?


(sorry for send it again because of the uncertain first 
sending failure to this list)


Sophie

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: vlookup by perl instead of excel

2006-02-02 Thread Hal
On Thu, 2006-02-02 at 16:52 -0800, Sanjay Chandriani wrote:
> Hi,
> 
> Ok, so I see my question was unclearly posed.  Here's what I want to do:
> 
> I have two tab-delimited text files.
> 
> File one:
>   column 1: UNIQUE identifiers (about 40,000), one identifier per line.
>   column 2:  some description of the identifier (let's say, red, green or 
> blue).
> 
>   For example, the 9th identifier down might be 'hat' and it's
> description in the next column over is 'blue'.
> 
> File two:
>   column 1:  a subset of the identifiers found in file one, column 1
> (about 1,000)
> 
> This is what I would like to do:
> 
> Make a second column in file 2 with the correct description to the
> identifier as described in the master file (file 1).
> 
> So, in the end, my second file should have the following TWO columns:
>   column 1:  a subset of the identifiers found in file one, column 1
> (about 1,000)
>   colum 2:  the appropriate description (red, green or blue) as
> instructed by file 1.
> 
> vlookup in excel can do this for me, but it is very slow.  So, if i
> had a perl script that can let me achieve my goal, i am sure it will
> be faster.  Any help would be greatly appreciated.

Hey Sanjay,
A vlookup is a poor person's hash.
First you need to get hold of your data somehow, eg reading it in from a
csv file or something.

my %vlookup;


$vlookup{'foo'} = "bar";
$vlookup{'item1'} = 1.25;
$vlookup{'code'} = 'abcde';


# now we have a hash structure filled with values you can get at by key
# so $vlookup{'foo'} will return the value associated with foo, ie 'bar'
in this case

print "\$vlookup{'foo'} is ", $vlookup{'foo'}, "\n";

it gets more complex than this when you want to keep a bunch of things
associated with a unique id. eg using only the id you can get say the
price, the qty remaining, the original manufacturer, a short description
why we won't sell this to customer A under any circumstances etc.
bash$ perldoc perldsc
has more of the details you require. 

Other things to do is search CPAN for Spreadsheet modules to read and
write in .xls format if you require this, or alternatively research how
to reliably read csv files, which isn't quite as easy as it might first
appear - however with that caveat I'm sure you'll find a recipe :)

HTH
Hal


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: question regarding reference of a local variable

2006-02-02 Thread Chas Owens
On 2/2/06, Pine Yan <[EMAIL PROTECTED]> wrote:
>
> Hi, what happens to the memory space with the following code:
>
> my @full_list = ();
>
> if (...) {
>   my @tmp_list;
>   @tmp_list = split(...);
>   @full_list = (@full_list, [EMAIL PROTECTED]);
> }
>
>
> Here @tmp_list is a local variable and its definition terminates at the
> end of this code segment. Does this mean the
> memory space it occupies will be freed, too? If yes, what will happen to
> the reference of that variable stored in @full_list?
>
> Thanks.
>
> Sincerely
> Pine

Perl uses reference count garbage collection.  This means that as long
as a reference to the memory that was allocated by my exists it sticks
around.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




question regarding reference of a local variable

2006-02-02 Thread Pine Yan

Hi, what happens to the memory space with the following code:

my @full_list = ();

if (...) {
  my @tmp_list;
  @tmp_list = split(...);
  @full_list = (@full_list, [EMAIL PROTECTED]);
}


Here @tmp_list is a local variable and its definition terminates at the
end of this code segment. Does this mean the
memory space it occupies will be freed, too? If yes, what will happen to
the reference of that variable stored in @full_list?

Thanks.

Sincerely
Pine



Re: vlookup by perl instead of excel

2006-02-02 Thread Sanjay Chandriani
Hi,

Ok, so I see my question was unclearly posed.  Here's what I want to do:

I have two tab-delimited text files.

File one:
  column 1: UNIQUE identifiers (about 40,000), one identifier per line.
  column 2:  some description of the identifier (let's say, red, green or blue).

  For example, the 9th identifier down might be 'hat' and it's
description in the next column over is 'blue'.

File two:
  column 1:  a subset of the identifiers found in file one, column 1
(about 1,000)

This is what I would like to do:

Make a second column in file 2 with the correct description to the
identifier as described in the master file (file 1).

So, in the end, my second file should have the following TWO columns:
  column 1:  a subset of the identifiers found in file one, column 1
(about 1,000)
  colum 2:  the appropriate description (red, green or blue) as
instructed by file 1.

vlookup in excel can do this for me, but it is very slow.  So, if i
had a perl script that can let me achieve my goal, i am sure it will
be faster.  Any help would be greatly appreciated.

Sanjay

On 2/2/06, Chris Devers <[EMAIL PROTECTED]> wrote:
> On Thu, 2 Feb 2006, Sanjay Chandriani wrote:
>
> > I am a perl newbie, so pardon my question if it is super easy.  I used
> > to routinely use Microsoft excel to do my vlookup's on a PC.
> > Recently, I got a new Apple computer and the vlookup function on Mac's
> > excel is unbearably slow.  I often do vlookup's for a column of
> > identifiers that is 40K long. This can take 30 minutes!!  I figured
> > there has to be a perl script out there that will do this for me, but
> > I haven't been able to find one by google-ing.  I wish i could write
> > it myself, but I am only on the 3rd chapter of the llama book!  :)
> >
> > Any help would be greatly appreciated.
>
> Part of the trick in asking good questions is to explain any jargon.
>
> What's a vlookup ?
>
> The Excel help file says, in part:
>
> VLOOKUP
> Searches for a value in the leftmost column of a table, and then
> returns a value in the same row from a column you specify in the
> table. Use VLOOKUP instead of HLOOKUP when your comparison values
> are located in a column to the left of the data you want to find.
>
> By contrast...
>
> HLOOKUP
> Searches for a value in the top row of a table or an array of
> values, and then returns a value in the same column from a row you
> specify in the table or array. Use HLOOKUP when your comparison
> values are located in a row across the top of a table of data, and
> you want to look down a specified number of rows. Use VLOOKUP when
> your comparison values are located in a column to the left of the
> data you want to find. The H in HLOOKUP stands for "Horizontal."
>
> Ok, so... your data is in Excel, and you're working on a Mac, and you
> want to do this vlookup operation or an equivalent using Perl? Yes?
>
> I suspect it will be easier to help you if you give us a clear idea of
> what your data is, how it's being stored (Excel, or other), what you
> need to accomplish with your data, and -- most important -- what you
> have tried so far.
>
>
>
> --
> Chris Devers
> DO NOT LEAVE IT IS NOT REAL
>

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: vlookup by perl instead of excel

2006-02-02 Thread Chris Devers
On Thu, 2 Feb 2006, Sanjay Chandriani wrote:

> I am a perl newbie, so pardon my question if it is super easy.  I used 
> to routinely use Microsoft excel to do my vlookup's on a PC.  
> Recently, I got a new Apple computer and the vlookup function on Mac's 
> excel is unbearably slow.  I often do vlookup's for a column of 
> identifiers that is 40K long. This can take 30 minutes!!  I figured 
> there has to be a perl script out there that will do this for me, but 
> I haven't been able to find one by google-ing.  I wish i could write 
> it myself, but I am only on the 3rd chapter of the llama book!  :)
> 
> Any help would be greatly appreciated.

Part of the trick in asking good questions is to explain any jargon.

What's a vlookup ?

The Excel help file says, in part:

VLOOKUP
Searches for a value in the leftmost column of a table, and then 
returns a value in the same row from a column you specify in the 
table. Use VLOOKUP instead of HLOOKUP when your comparison values 
are located in a column to the left of the data you want to find.

By contrast...

HLOOKUP
Searches for a value in the top row of a table or an array of 
values, and then returns a value in the same column from a row you 
specify in the table or array. Use HLOOKUP when your comparison 
values are located in a row across the top of a table of data, and 
you want to look down a specified number of rows. Use VLOOKUP when 
your comparison values are located in a column to the left of the 
data you want to find. The H in HLOOKUP stands for "Horizontal."

Ok, so... your data is in Excel, and you're working on a Mac, and you 
want to do this vlookup operation or an equivalent using Perl? Yes?

I suspect it will be easier to help you if you give us a clear idea of 
what your data is, how it's being stored (Excel, or other), what you 
need to accomplish with your data, and -- most important -- what you 
have tried so far.



-- 
Chris Devers
DO NOT LEAVE IT IS NOT REAL

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




vlookup by perl instead of excel

2006-02-02 Thread Sanjay Chandriani
Hi,

I am a perl newbie, so pardon my question if it is super easy.  I used to
routinely use Microsoft excel to do my vlookup's on a PC.  Recently, I got a
new Apple computer and the vlookup function on Mac's excel is unbearably
slow.  I often do vlookup's for a column of identifiers that is 40K long.
This can take 30 minutes!!  I figured there has to be a perl script out
there that will do this for me, but I haven't been able to find one by
google-ing.  I wish i could write it myself, but I am only on the 3rd
chapter of the llama book!  :)

Any help would be greatly appreciated.

Thanks.
Sanjay


RE: Sorting a hash of hashes

2006-02-02 Thread Wagner, David --- Senior Programmer Analyst --- WGO
Scott Palmer wrote:
> I am attempting to sort by a field in a hash within a hash and I am
> having a hard time finding the right direction. I want the print out
> to sort from smallest to largest in size. Any help would be greatly
> appreciated.
> 
> Scott
> 
> --
> #!/usr/bin/perl
> 
> # Set your size/time requirements
> $minsize = 50;  # Expressed in Megabytes
> $maxtime = 90;  # Expressed in days
> 
> # Define modules we need
> use File::Find;
> use Time::Local;
> 
> # Get defined inputs and convert them to something we can use
> $nosmaller = ($minsize * 1024);
> $nonewer = ($maxtime * 86400);
> $time = timelocal($seconds, $minutes, $hours, (localtime)[3,4,5]);
> $timecomp = ($time - $nonewer);
> 
> %tmp = ();
> # setup temp hash
> $count = 0;
> 
> sub wanted {
> my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
> $size, $atime, $mtime, $ctime,) = stat($_);
> if ($atime < $timecomp) {
> if ($size > $nosmaller) {
> $count++;
> my $filename = $File::Find::name;
> my ($name) = getpwuid($uid);
> my $osize = ($size / 1024 / 1024);
> $tmp{$count}{owner} = $name;
> $tmp{$count}{size} = $osize;
> $tmp{$count}{file} = $filename;
> }
> }
> }
> 
> File::Find::find({wanted => \&wanted}, @ARGV);
> 
> # Print out hash table
> for $count ( keys %tmp ) {
chg to
  for $count ( sort {$tmp{$a}{size} <=> $tmp{$b}{size}} keys %tmp )
which should do a numerical sort verse an alpha sort for you.
Wags ;)

> printf "%10s %12.2f MB $tmp{$count}{file} \n",
> $tmp{$count}{owner}, $tmp{$count}{size};
> }
> exit;



***
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
***


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Sorting a hash of hashes

2006-02-02 Thread Scott Palmer
I am attempting to sort by a field in a hash within a hash and I am
having a hard time finding the right direction. I want the print out to
sort from smallest to largest in size. Any help would be greatly
appreciated. 

Scott

--
#!/usr/bin/perl

# Set your size/time requirements
$minsize = 50;  # Expressed in Megabytes
$maxtime = 90;  # Expressed in days

# Define modules we need
use File::Find;
use Time::Local;

# Get defined inputs and convert them to something we can use
$nosmaller = ($minsize * 1024);
$nonewer = ($maxtime * 86400);
$time = timelocal($seconds, $minutes, $hours, (localtime)[3,4,5]);  
$timecomp = ($time - $nonewer); 

%tmp = ();
# setup temp hash
$count = 0;

sub wanted {
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
$size, $atime, $mtime, $ctime,) = stat($_);
if ($atime < $timecomp) {
if ($size > $nosmaller) {
$count++;
my $filename = $File::Find::name;
my ($name) = getpwuid($uid);
my $osize = ($size / 1024 / 1024);
$tmp{$count}{owner} = $name;
$tmp{$count}{size} = $osize;
$tmp{$count}{file} = $filename;
}
}
}

File::Find::find({wanted => \&wanted}, @ARGV);

# Print out hash table
for $count ( keys %tmp ) {
printf "%10s %12.2f MB $tmp{$count}{file} \n",
$tmp{$count}{owner}, $tmp{$count}{size};
}
exit;


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Script's command-line options under Windows

2006-02-02 Thread Bakken, Luke
Daniel Kasak wrote:
> Timothy Johnson wrote:
>> It sounds like there is a problem with your file association.
>> 
>> Open HKEY_CLASSES_ROOT and go to Perl-->Shell-->Open-->Command.
>> 
>> The (Default) entry should probably read:
>> 
>> "C:\Perl\bin\perl.exe" "%1" %*
>> 
> I'll check it out tomorrow when I can get back to the PC.
> Thanks :)

This is most likely the problem. Type "assoc .pl" to get the perl type
(probably Perl) and then "ftype Perl" to see the associated command:

C:\>assoc .pl
.pl=Perl

C:\>ftype Perl
Perl=c:\perl\bin\perl.exe "%1" %*

Also, you can add .PL to the PATHEXT variable if you don't want to have
to use the extension to run the script in the future.

Luke

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Closing and Re-Opening the Same File Problem

2006-02-02 Thread Tom Phoenix
On 2/2/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

> Apparently, the statement "@Row =  split(/\s+/, $TheRec);" is not
> recognizing the blanks between the fields in the input record.  Are they  
> something
> other than blanks?

That's a good question to ask the debugger. The 'x' command in the
debugger will evaluate any perl expression, so you could do something
like 'x $TheRec', or even 'x split(/\s+/, $TheRec)', once you've
reached the right point at runtime. The perldebug manpage has the
details.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Closing and Re-Opening the Same File Problem

2006-02-02 Thread John Doe
[EMAIL PROTECTED] am Donnerstag, 2. Februar 2006 19.35:
> Here's my problem:
>
> I am writing a program that opens one file  (pricedata.csv)  as input, does
> some processing, and opens a new file  (PriceDataRfrmtd1.txt)  for ouput.
> Subsequently I close both files.  So far so good. Now, within the same
> program, I attempt to re-open the previous  output (i.e.
> PriceDataRfrmtd1.txt) as input and perform some processing and I  begin to
> run into some problems. More specifically, here's a segment of my  code:

This shouldn't be any problem. I guess the problem lies in your code.
Please provide the relevant code; the one below contains no opening/closing of 
files, and...


>
>
> while() {
>
>   $TheRec = $_;
> chomp($TheRec);
>
>   @Row =  split(/\s+/, $TheRec);
>
>   print "[EMAIL PROTECTED] = @Row\n";
>
>   $Date = $Row[0];
> $AdjOpen = $Row[2];
> $AdjClose =  $Row[4];
>
>
> print "\n\$Date = $Date\n";
> print "\n\$AdjOpen =  $AdjOpen\n";
> print "\n\$AdjClose =  $AdjClose\n";
>
> exit;

...exits the program after having read the first line of .


> } # End of while()

[snipped description of first array element conaining whole line]


hth
joe

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Closing and Re-Opening the Same File Problem

2006-02-02 Thread Sanbuah
Here's my problem:
 
I am writing a program that opens one file  (pricedata.csv)  as input, does 
some processing, and opens a new file  (PriceDataRfrmtd1.txt)  for ouput.  
Subsequently I close both files.  So far so good. Now, within the same program, 
I 
attempt to re-open the previous  output (i.e. PriceDataRfrmtd1.txt) as input 
and perform some processing and I  begin to run into some problems. More 
specifically, here's a segment of my  code:
 
 
while() {
 
  $TheRec = $_;
chomp($TheRec);
 
  @Row =  split(/\s+/, $TheRec);
 
  print "[EMAIL PROTECTED] = @Row\n";
 
  $Date = $Row[0];
$AdjOpen = $Row[2];
$AdjClose =  $Row[4];


print "\n\$Date = $Date\n";
print "\n\$AdjOpen =  $AdjOpen\n";
print "\n\$AdjClose =  $AdjClose\n";

exit;


} # End of while()
 
 
The first input record from the file "PriceDataRfrmtd1." is as  follows:
 
02-01-93  43.97  35.29  44.25  35.51 1.246
 
The List @Row should contain 6 entries.  
 
$Date = $Row[0] should equal 02-01-93.

$AdjOpen = $Row[2] should equal 35.29.

$AdjClose = $Row[4] should equal 35.51.
 
However, when I run the program I am getting the following results:
 
@Row appears to contain only one entrynamelythe entire input  record:
 
02-01-93   43.97   35.29   44.25   35.51  1.246
 
$Date = $Row[0] equals this first and only entry:
 
02-01-93   43.97   35.29   44.25   35.51  1.246
 
while both $AdjOpen = $Row[2]  and  $AdjClose = $Row[4] equal  "".that 
is...they are empty.
 
Apparently, the statement "@Row =  split(/\s+/, $TheRec);" is not  
recognizing the blanks between the fields in the input record.  Are they  
something 
other than blanks?
 
Originally, I wrote two separate programs to perform this function.   That is 
I wrote program one to read in input file "pricedata.txt" and write it  out 
to "PriceDataRfrmtd.txt". Next I wrote a second program to read  
"PriceDataRfrmtd.txt" as input, do some processing and write it out to a  new 
reformatted 
output file.  However, when I decided to combine  both programs, I began to run 
into this probelm. I am running this Perl  program in DOS under the Windows XP 
operating system.  Do you know what the  problem is?
 
Thanks,
 
Frank







Re: how to share variable across multi-processes

2006-02-02 Thread Jay Savage
On 1/31/06, zentara <[EMAIL PROTECTED]> wrote:
>
> >Since the actions of 'fork' and 'tie' happen so frequently,is there any 
> >performance drawback to the program?thanks again.
>
> Well, in reality, probably no one reading this list knows for sure.
> Set up your script and run it, and see if it seems to bog down.
>
> IPC thru shared memory is the fastest available, but it can cause some
> odd underlying problems (which you may, or may not see). The problem
> comes from the strict buffer sizes when using shared memory. In Perl
> we are used to saying "store this", and we know Perl handles it
> auto-magically.
> But when using shared memory, if the data is bigger than the memory
> segment assigned to store it, you may get bad results, ranging from your
> data being truncated, to it overrunning the data in
> the adjacent segment.  You can also get extra hex characters appended
> to your data, if your data is shorter than the segment size. Now I don't
> know how the various modules handle this, but it is a big problem.
> Which is why I usually just go with threads and shared variables,
> although I usually don't care too much about speed, and I seldom
> have to deal with "forking-on-demand". Threads are better suited when
> you know the max number of threads to be used, then you can declare
> all the shared variables.
>
> So if you can be sure that your data won't exceed the predefined shared
> memory segment sizes, it will probably work well for you. You also could
> work out a scheme to save long data across multiple segments.
>

Can you explain this a little more? I haven't done much with
IPC::Shareable or Share::Light--but it looks like I'm about to get
into them for a project, and I have two questions.

First, It's my understanding that Shareable and ShareLite are complete
implementations that rely on SysV shared memory, not simple wrappers
for the system calls, i.e. they allocate space on the heap and/or
stack for themselves and then dynamically parcel out that space on
demand. The modules should do the heavy lifting of managing the
pointers and return the correct value to the caller. If you have to
manage everything yourself anyway, why not just use inline C to pass
the pointers around by hand instead of running the overhead of another
module load?

Second, under what circumstances would you get bad data? And are the
read-past and write-past errors you're describing in memory, or the
modules' internal data structures? Any modern OS should return SEGV on
any attempt to write past the end of the page. An atempt to read
beyond the end of the page should generate SEGV, too, although most of
the time it doesn't, it just returns fewer bytes than expected, except
on recent OBSD. Your data still shouldn't be truncated, though,
because if it was too long, the attempted overflow should have
produced a SEGV when the write attempt was made. Or does
IPC::Shareable trp SEGV internally and then return unpredictable
values to the caller? Allowing "extra bytes," i.e. whatever was lying
around in the buffer before, to be passed back to a caller strikes me
a pretty serious, potentially exploitable flaw, too. Or maybe I'm
misunderstanding what you wrote?

Thanks,

-- jay
--
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

values of β will give rise to dom!


Re: How to compare hashes to find matching keys with conflicting values.

2006-02-02 Thread John Doe
Angus am Donnerstag, 2. Februar 2006 10.14:
> John,

Hello Angus

> Well that works perfect now my issue is to understand what you have
> provided for me.  I see that we are creating an array called res however I
> am not sure what map is doing.  I did read the perldoc info on map but I am
> still confused on how it is being used here.  I see from the perldoc that I
> could use it like this
>
> %register = map {$register($) => $_ @array;
> print "@array\n";

No, this is syntactically incorrect and wouldn't do anything useful.

> This would I think create an array based on the values contained in the
> register hash.  

No; in an assignement

  = 

the left part is made from the expression on the right.

> But, I would have now way to link them back to the keys
> from register as now they would be numerically ordered in the array instead
> of being linked to the hostname key.
>
> So I am looking at your example:
> %actual = map ($actual{$_} eq $register{$_})

This is not my example... and syntactically incorrect.

> This I think says each key eq to the other key value.  

The part within the braces does a comparison between $actual{$_} and 
$register{$_}. Result is true or false.


> Next you use a ? mark which I understand means that the previous 
> values are optional, 

No, its part of the ternary ?: operator (see perldoc perlop).

my $var = $x ? $y : $z

is the same as

my $var;
if ($x) {
  $var=$y;
}
else {
$var=$z;
}

> which  
> in this case if the are not equal are they then not included in the $_
> variable.  But if they are eq then they are included in the $_ value which
> means they are placed back into the hashes?  After this the register hash
> is sorted but I don't understand why.

It's because of the sort (see perldoc -f sort) in the expression. The keys of 
%keys are sorted with it before it goes through the "map pipe".

> The next code block iterates through the res array and unless it finds a
> reference?  I did a perldoc on ref and see that it returns a non-empty
> string if EXPR is a reference.  So now I think we created an array of
> hashes with the @res and map combo, is that correct?

Ok, let's analyse in detail:

[...]
> my %actual = (
> "host1" => "164.72.119.175",
> "host2" => "164.72.123.43",
> "host3" => "164.72.45.98",
> );
>
> my %register = (
> "host1" => "164.72.119.179",
> "host2" => "164.72.21.43",
> "host3" => "164.72.45.98",
> );
[citation removed, reformatted:]

my @res = map {
   ( $actual{$_} eq $register{$_} )
   ? $_
   : [ $actual{$_}, $register{$_} ]
} sort keys %register;

The whole expression is to read from bottom to top; %register is the start; it 
is handled in 3 ways each after the other, resulting in @res.

1st, the keys are taken (keys %register).
2nd, the keys are sorted (sort)
3rd, map creates a new structure.

You could image some sort of "pipe" with stations:
entity:@res <- do:map <- do:sort <- do:keys <- entity:%register

Note that @res does nowhere occur on the right side: It's the result; nothing 
is inside it before the expression on the right side of the '=' is evaluated.


Now to the map part: ... <-- map {  } <-- ... : 
It takes something at the right, mangles it, and puts it out on the left.
(Note the {}-braces are part of the map; it's one of it's two syntax).

Within map{}, $_ contains the sorted keys of %register; one by one is mangeld 
by map{}. $_ is a scalar, fed to map on the right, and map returns a scalar 
on the left.

The scalar is either the key ( $_ ) or an arrayref [ ... ], depending of the 
result of the comparison, taken from after ? or after : .

What the map does here (for every $_ separate) in words:
"Compare the value from %actual keyed by $_ with the value from %register 
keyed by $_. If they are equal (means: if the IPs are equal), return the key 
(means: hostname); otherwise, return the two different values (means: the two 
different IPs) within an arrayref."


I hope this helps and my english was not too confusing.

Just play around with map to get the feeling. I remember that it is not easy 
to understand at the beginning, but if you get the idea, you won't miss it, 
because it allows to do complex actions in a very short way.

joe



[nothing new below]
> foreach my $e(@res) {
> unless(ref($e)) {
> print "$e is ok\n";
> }
> else {
> print "$e differs:actual @{[$e->[0]]} <-> register @{[$e->[1]]}\n";
> }
> }
>
>
> -Original Message-
> From: John Doe [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, February 01, 2006 2:45 AM
> To: beginners@perl.org
> Subject: Re: How to compare hashes to find matching keys with conflicting
> values.
>
> Sorry for another mail, but there are too many typos:
> > Also untested:
> >
> > # If no conflict, value is hostname.
> > # If conflict, value is arrayref with the two different IPs
> > #
> > my @res = map {
> >   ($actual{$_} eq $register{$_})
> >   ? $_
> >
> >   : [$actual{$_}, $register{$_}]
> >
> > } sort keys %register;
> >
> > foreach my $e (@res) {
> >   if (ref($e)) {
>
> This

RE: How to compare hashes to find matching keys with conflicting values.

2006-02-02 Thread Angus
John,

Well that works perfect now my issue is to understand what you have provided
for me.  I see that we are creating an array called res however I am not
sure what map is doing.  I did read the perldoc info on map but I am still
confused on how it is being used here.  I see from the perldoc that I could
use it like this

%register = map {$register($) => $_ @array;
print "@array\n";

This would I think create an array based on the values contained in the
register hash.  But, I would have now way to link them back to the keys from
register as now they would be numerically ordered in the array instead of
being linked to the hostname key.

So I am looking at your example:
%actual = map ($actual{$_} eq $register{$_})  
This I think says each key eq to the other key value.  Next you use a ? mark
which I understand means that the previous values are optional, which in
this case if the are not equal are they then not included in the $_
variable.  But if they are eq then they are included in the $_ value which
means they are placed back into the hashes?  After this the register hash is
sorted but I don't understand why.  

The next code block iterates through the res array and unless it finds a
reference?  I did a perldoc on ref and see that it returns a non-empty
string if EXPR is a reference.  So now I think we created an array of hashes
with the @res and map combo, is that correct?  

Thanks in advance if you decide to try and tackle my confused questions and
explain how this works but if you don't want to I understand and still very
much appreciate your help.

Thanks,

-angus

#!/usr/bin/perl
#
use strict;
use warnings;

system "clear";

my %actual = (
"host1" => "164.72.119.175",
"host2" => "164.72.123.43",
"host3" => "164.72.45.98",
"host4" => "164.72.98.89",
"host5" => "164.72.67.123",
);

my %register = (
"host1" => "164.72.119.179",
"host2" => "164.72.21.43",
"host3" => "164.72.45.98",
"host4" => "164.72.8.89",
"host5" => "164.72.67.123",
);

my @res = map { 
($actual{$_} eq $register{$_})
? $_
:[$actual{$_}, $register{$_}]
} sort keys %register;

foreach my $e(@res) {
unless(ref($e)) {
print "$e is ok\n";
}
else {
print "$e differs:actual @{[$e->[0]]} <-> register @{[$e->[1]]}\n";
}
}


-Original Message-
From: John Doe [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, February 01, 2006 2:45 AM
To: beginners@perl.org
Subject: Re: How to compare hashes to find matching keys with conflicting
values.

Sorry for another mail, but there are too many typos:

> Also untested:
>
> # If no conflict, value is hostname.
> # If conflict, value is arrayref with the two different IPs
> #
> my @res = map {
>   ($actual{$_} eq $register{$_})
>   ? $_
>   : [$actual{$_}, $register{$_}]
>
> } sort keys %register;
>
> foreach my $e (@res) {
>   if (ref($e)) {

This should read
   unless (ref($e)) {

> print "$e is ok\n";
>   }
>   else {
> print "$e differs: actual @{[$e->[0]]} <-> register @{[$e->[1]]}\n";
>   }
> }
>
> I think this should give you an idea.
> To look op any perl funktion, type:

up, function

>
> perldoc -f FUNCTIONNAME
>
> The man page for operaters:

operators

Nice day to all :-)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]