Conditional sorting across two arrays

2006-12-12 Thread Rinku Mahesh
  Hi,
   
 I've two arrays of same depth (let it be 6)
   
  1st Array:-  @unique {11 , 23, 44, 66, 900, 1009}
  2nd Array:- @occ_count {2, 77, 22, 2, 77,29}
   
  Here I'm looking for a sorting mechanism with the following conditions:-
   
  a. Sort 2nd Array in descending order such that it should reflect 
{77,77,29,22,2,2} 
   
  b. The values of 1st array should also change in accordance with the 
positions of elements of 2nd array such that sorting the 1st array should 
reflect {23,900,1009,44,11,66}
   
  23 and 900 or 11 and 66 can be interchanged but whatever value is lesser 
should appear first in the list.
   
  c. As there are repeatations in 2nd Array (77 and 2) there could be two 
values associated with these elements in 1st array (23/900 or 11/66). Here 23 
is lesser than 900 thus it should appear fist in the list.
   
   
  If the above explaination is confusing I'm looking for a way where every 
element of an array can be mapped to corresponding element of another array and 
as a whole both the arrays require a sorting w.r.t. 2nd array.
   
   
  Can anyone suggest an eficient way to implement same.
   
  TIA,
   
  Rinku


 
-
Everyone is raving about the all-new Yahoo! Mail beta.

Re: Conditional sorting across two arrays

2006-12-12 Thread Rob Dixon

Rinku Mahesh wrote:
> Hi,
>
> I've two arrays of same depth (let it be 6)
>
> 1st Array:-  @unique {11 , 23, 44, 66, 900, 1009} 2nd Array:- @occ_count {2,
> 77, 22, 2, 77,29}
>
> Here I'm looking for a sorting mechanism with the following conditions:-
>
> a. Sort 2nd Array in descending order such that it should reflect
> {77,77,29,22,2,2}
>
> b. The values of 1st array should also change in accordance with the
> positions of elements of 2nd array such that sorting the 1st array should
> reflect {23,900,1009,44,11,66}
>
> 23 and 900 or 11 and 66 can be interchanged but whatever value is lesser
> should appear first in the list.
>
> c. As there are repeatations in 2nd Array (77 and 2) there could be two
> values associated with these elements in 1st array (23/900 or 11/66). Here 23
> is lesser than 900 thus it should appear fist in the list.
>
>
> If the above explaination is confusing I'm looking for a way where every
> element of an array can be mapped to corresponding element of another array
> and as a whole both the arrays require a sorting w.r.t. 2nd array.
>
>
> Can anyone suggest an eficient way to implement same.

Hi Rinku

You need to sort a list of indices. Take a look at the code below which does
what you describe.

HTH,

Rob


use strict;
use warnings;

my @unique = (11, 23, 44, 66, 900, 1009);
my @occ_count = (2, 77, 22, 2, 77, 29);

my @idx = sort {
  $occ_count[$b] <=> $occ_count[$a] or
  $unique[$a] <=> $unique[$b]
} 0 .. $#unique;

@unique = @[EMAIL PROTECTED];
@occ_count = @[EMAIL PROTECTED];

foreach (0 .. $#unique) {
  printf "%4d - %2d\n", $unique[$_], $occ_count[$_];
}

**OUTPUT**

  23 - 77
 900 - 77
1009 - 29
  44 - 22
  11 -  2
  66 -  2


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




Re: Conditional sorting across two arrays

2006-12-12 Thread Rob Coops

Hi Rinku,

Now I could try and explain this in my own words but I think this will help
you a lot more.

http://www.unix.org.ua/orelly/perl/advprog/ch02_02.htm

What you are looking for is a very common thing, your not the first to bump
into this problem :-)

Regards,

Rob



On 12/12/06, Rinku Mahesh <[EMAIL PROTECTED]> wrote:


Hi,

I've two arrays of same depth (let it be 6)

1st Array:-  @unique {11 , 23, 44, 66, 900, 1009}
2nd Array:- @occ_count {2, 77, 22, 2, 77,29}

Here I'm looking for a sorting mechanism with the following conditions:-

a. Sort 2nd Array in descending order such that it should reflect
{77,77,29,22,2,2}

b. The values of 1st array should also change in accordance with the
positions of elements of 2nd array such that sorting the 1st array should
reflect {23,900,1009,44,11,66}

23 and 900 or 11 and 66 can be interchanged but whatever value is lesser
should appear first in the list.

c. As there are repeatations in 2nd Array (77 and 2) there could be two
values associated with these elements in 1st array (23/900 or 11/66). Here
23 is lesser than 900 thus it should appear fist in the list.


If the above explaination is confusing I'm looking for a way where every
element of an array can be mapped to corresponding element of another array
and as a whole both the arrays require a sorting w.r.t. 2nd array.


Can anyone suggest an eficient way to implement same.

TIA,

Rinku



-
Everyone is raving about the all-new Yahoo! Mail beta.



Re: Conditional sorting across two arrays

2006-12-12 Thread Dr.Ruud
Rinku Mahesh schreef:

>  I've two arrays of same depth (let it be 6)
> 
>   1st Array:-  @unique {11 , 23, 44, 66, 900, 1009}
>   2nd Array:- @occ_count {2, 77, 22, 2, 77,29}
> [...]
>   b. The values of 1st array should also change in accordance with
> the positions of elements of 2nd array such that sorting the 1st
> array should reflect {23,900,1009,44,11,66}  

Why then are these values not stored in an AoA? (see perldsc)

  my @x =
  ( [  11,  2]
  , [  23, 77]
  , [  44, 22]
  , [  66,  2]
  , [ 900, 77]
  , [1009, 29]
  ) ;

-- 
Affijn, Ruud

"Gewoon is een tijger."

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




Re: Conditional sorting across two arrays

2006-12-12 Thread Ovid
--- Rob Coops <[EMAIL PROTECTED]> wrote:

> Hi Rinku,
> 
> Now I could try and explain this in my own words but I think this
> will help you a lot more.
> 
> http://www.unix.org.ua/orelly/perl/advprog/ch02_02.htm
> 
> What you are looking for is a very common thing, your not the first
> to bump into this problem :-)

Please don't link to copyrighted work which has been posted to the Web
in violation of said copyright.  Writing a book is hard work and unless
the author deliberately wants their book to be given away for free, we
should respect the author's desire to earn a living.

Cheers,
Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/

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




RE: Conditional sorting across two arrays

2006-12-12 Thread Charles K. Clarkson
Rinku Mahesh  wrote:

:   If the above explaination is confusing I'm looking for a way
: where every element of an array can be mapped to corresponding
: element of another array and as a whole both the arrays require
: a sorting w.r.t. 2nd array.

M.J. Dominus wrote three slides about an Indirect Sort which
you may find helpful. He's using two arrays of names. One for
first names, one for last names. Basically, the idea is to sort
the array indexes and then apply the sorted indexes to each array
using an array slice. (I don't know if he's right about those APL
folks.)

http://perl.plover.com/yak/hw2/samples/slide003.html


In your case, you need a numerical comparison (<=>) on both
arrays as opposed to a string comparison (cmp). You also require
that ties be sorted on the other array. That can be performed
within the sort. One thing I like about sorting indexes is that
you can save the sorts.

use strict;
use warnings;

# Test data
my @unique= ( 66, 23, 44, 11, 900, 1009 );
my @occ_count = (  2, 77, 22,  2,  77,   29 );

# Sort in ascending order, ascending order
# Use reverse for descending order, descending order
my @ascending_indexes =
sort {
$occ_count[$a] <=> $occ_count[$b]
||
   $unique[$a] <=> $unique[$b]

} 0 .. $#occ_count;

# Sort in descending order, ascending order
# Use reverse for ascending order, descending order
my @descending_indexes =
sort {
$occ_count[$b] <=> $occ_count[$a]
||
   $unique[$a] <=> $unique[$b]

} 0 .. $#occ_count;


# Reports
print "Ascending sort then ascending sort:\n\t";
printf '%5d', $_ foreach @occ_count[ @ascending_indexes ];
print "\n\t";
printf '%5d', $_ foreach @unique[ @ascending_indexes ];
print "\n\n";

print "Ascending sort then descending sort\n\t";
printf '%5d', $_ foreach @occ_count[ reverse @descending_indexes ];
print "\n\t";
printf '%5d', $_ foreach @unique[ reverse @descending_indexes ];
print "\n";

print "Descending sort then descending sort:\n\t";
printf '%5d', $_ foreach @occ_count[ reverse @ascending_indexes ];
print "\n\t";
printf '%5d', $_ foreach @unique[ reverse @ascending_indexes ];
print "\n\n";

print "Descending sort then ascending sort\n\t";
printf '%5d', $_ foreach @occ_count[ @descending_indexes ];
print "\n\t";
printf '%5d', $_ foreach @unique[ @descending_indexes ];
print "\n";

__END__

For larger arrays, you might want to transform one array
first.


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
Free Market Advocate
Web Programmer

254 968-8328

http://www.clarksonenergyhomes.com/

Don't tread on my bandwidth. Trim your posts.


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




Re: Conditional sorting across two arrays

2006-12-12 Thread Paul
On Tue, December 12, 2006 6:25 am, Ovid wrote:
> --- Rob Coops <[EMAIL PROTECTED]> wrote:
>
>> Hi Rinku,
>> Now I could try and explain this in my own words but I think this will
help you a lot more.
>>
>> What you are looking for is a very common thing, your not the first to
bump into this problem :-)
>
> Please don't link to copyrighted work which has been posted to the Web
in violation of said copyright.  Writing a book is hard work and unless
the author deliberately wants their book to be given away for free, we
should respect the author's desire to earn a living.

It was also linked it in your reply, which was a violation of your violation.





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




module to calculate time difference between two

2006-12-12 Thread kilaru rajeev

Hi All,

I want to calculate the time difference(hh:mm:ss) between two when they are
provided with date and time. Could anyone provide the module to handle this?

--
Thanks and Regards,
Rajeev Kilaru
Associate
Charles River Team
Franklin Templeton Technologies (FTT-ISC)
Hyderabad
Work Pone: 040-66100303 Extn: 3235
Mobile: +919849544332


Re: module to calculate time difference between two

2006-12-12 Thread Mathew
kilaru rajeev wrote:
> Hi All,
> 
> I want to calculate the time difference(hh:mm:ss) between two when they are
> provided with date and time. Could anyone provide the module to handle
> this?
> 

Maybe this can get you started:
http://beta.nntp.perl.org/group/perl.datetime/2006/05/msg6342.html

Mathew


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




Re: module to calculate time difference between two

2006-12-12 Thread D. Bolliger
kilaru rajeev am Dienstag, 12. Dezember 2006 13:22:
> Hi All,

Hello

> I want to calculate the time difference(hh:mm:ss) between two when they are
> provided with date and time. Could anyone provide the module to handle
> this?

There are different modules to handle date/time tasks.

CPAN is a great help. You can search by keywords, check the manuals, look at 
the source - all this without installing anything:

http://search.cpan.org

Also check the archives of this mailinglist, and google, on how to use either 
of these possibilities.

Dani

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




Re: How do I count & skip the number of duplicate elements in an array

2006-12-12 Thread Peter Scott
On Mon, 11 Dec 2006 06:02:25 -0800, Rinku Mahesh wrote:
> -
>   my @fields =();
>   my @unique_elements=();
>   my %seen   = ();
>
>
>   foreach my $elem (@fields) {
>   next if $seen{ $elem }++;
>   push (@unique_elements, $elem);
> }
> -
>
>   What needs to be done to compute the count of skipped/duplicate elements ?

print "$_ seen $seen{$_} times\n" for grep { $seen{$_} > 1 } keys %seen;

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


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




declaring a zero size hash

2006-12-12 Thread Dukelow, Don
I'm trying to declare a zero size hash so a sub function can populate it and
be see by all other sub's.

my %loginHash();

But the "use strict" doesn't like it.  All examples of making a hash
structure is hard coded in the program or is made reading from a file.  When
I try to run the script all I get is syntax error near "%loginHash("
What am I missing?

Don Dukelow


smime.p7s
Description: S/MIME cryptographic signature


Re: Perl "tree" script

2006-12-12 Thread Jenda Krynicky
From: stic <[EMAIL PROTECTED]>
> Yeah, very very thank for help, now it`s making exactly what i want.
> but could you please explain what all  this one line do?
> 
> push @{ -d "$cesta/$_" ? [EMAIL PROTECTED] : [EMAIL PROTECTED] }, $_ for grep 
> !/\A\.\.?\z/,readdir $OBSAH;
> 
> I`m absolute begginer in perl and can`t decrypt this line:D and i`m
> very interested to. thanks a lot.

Ahoj,

in a bit less idiomatic Perl it could be.


my @dir_contents = readdir $OBSAH;
 # read all files

@dir_contents = grep !/\A\.\.?\z/, @dir_contents;
#or
@dir_contents = grep !/^\.\.?$/, @dir_contents;
 # remove the . and .. directory entries
 # I'd have to think for a minute myself before I'd remember 
 # what does the \A and \z mean.

foreach (@dir_contents) {
 if (-d "$cesta/) {
  push @obsah2, $_
 } else {
  push @obsah3, $_
 }
}

Jenda

= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


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




Re: declaring a zero size hash

2006-12-12 Thread Jeff Pang
Just write it like:

my %loginHash = ();

This should work.

-Original Message-
>From: "Dukelow, Don" <[EMAIL PROTECTED]>
>Sent: Dec 13, 2006 12:23 AM
>To: beginners@perl.org
>Subject: declaring a zero size hash
>
>I'm trying to declare a zero size hash so a sub function can populate it and
>be see by all other sub's.
>
>my %loginHash();
>


--
Books below translated by me to Chinese.
Practical mod_perl: http://home.earthlink.net/~pangj/mod_perl/
Squid the Definitive Guide: http://home.earthlink.net/~pangj/squid/

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




Re: declaring a zero size hash

2006-12-12 Thread Adriano Ferreira

On 12/12/06, Dukelow, Don <[EMAIL PROTECTED]> wrote:

I'm trying to declare a zero size hash so a sub function can populate it and
be see by all other sub's.

my %loginHash();


 my %loginHash;

should be enough.


But the "use strict" doesn't like it.


It is not "use strict" that does not like it. It is Perl itself --
this is a syntax error:

 $ perl -e 'my %h();'
 syntax error at -e line 1, near "%h("
 Execution of -e aborted due to compilation errors.


All examples of making a hash
structure is hard coded in the program or is made reading from a file.  When
I try to run the script all I get is syntax error near "%loginHash("
What am I missing?


Something like this might do what you want:


# read a file and store each line at a bucket of a hash
sub pop_hash {
  my $h = shift; # the hash ref
  my $f = shift; # the filehandle
  while (<$f>) {
  $h->{$.} = $_;
  }
  return $h; # but it was already changed in-place
}

my %h;
pop_hash(\%h, *STDOUT);
use Data::Dumper;
print Dumper(\%h);

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




Re: declaring a zero size hash

2006-12-12 Thread Tom Phoenix

On 12/12/06, Dukelow, Don <[EMAIL PROTECTED]> wrote:


I'm trying to declare a zero size hash so a sub function can populate it and
be see by all other sub's.

my %loginHash();


Maybe you mean this?

   my %loginHash = ();

But every new variable (which is what 'my' is declaring) starts out
empty, so all you really need is this:

   my %loginHash;

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

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




RE: declaring a zero size hash

2006-12-12 Thread Helliwell, Kim
I think you need to do:

my %loginhash = {};

Kim Helliwell
LSI Logic Corporation
Work: 408 433 8475
Cell: 408 832 5365
[EMAIL PROTECTED]
 
Please Note: My email address changed to [EMAIL PROTECTED] on Oct
14. The old email address ([EMAIL PROTECTED]) will stop working after
Jan 15, 2007. Please update your address book and distribution lists
accordingly. Thank you.
-Original Message-
From: Dukelow, Don [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, December 12, 2006 8:23 AM
To: beginners@perl.org
Subject: declaring a zero size hash

I'm trying to declare a zero size hash so a sub function can populate it
and
be see by all other sub's.

my %loginHash();

But the "use strict" doesn't like it.  All examples of making a hash
structure is hard coded in the program or is made reading from a file.
When
I try to run the script all I get is syntax error near "%loginHash("
What am I missing?

Don Dukelow

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




Re: declaring a zero size hash

2006-12-12 Thread Adriano Ferreira

On 12/12/06, Helliwell, Kim <[EMAIL PROTECTED]> wrote:

I think you need to do:

my %loginhash = {};


That's not right. {} is a hash ref, not a hash. It stands for a scalar value.

When you do that

   my %h = {}

or, for the same result,

   my %h = 1;
   my %h = "abacate";

you end with a hash with one pair, whose key is the given scalar and
the value is undef.

See the output of

  $ perl -MData::Dumper -e '%h = {}; print Dumper(\%h)'
  $VAR1 = {
'HASH(0x10240170)' => undef
  };


Kim Helliwell
LSI Logic Corporation
Work: 408 433 8475
Cell: 408 832 5365
[EMAIL PROTECTED]


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




RE: Perl create tcp connections

2006-12-12 Thread Ron McKeever

Thanks for the updates, I understand.

Then perhaps someone can help me get this script to set the tcp 
to SYN_SENT on 100 ports:

#!/usr/bin/perl
use Socket;

$count=3500;

while ($count<3601)
{
$count++;
$addr=sockaddr_in($count,inet_aton('localhost'));
socket(S,PF_INET,SOCK_STREAM,getprotobyname('tcp'));
connect(S,$addr);
}

What am I missing?? 

Thanks,
Ron


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




Re: Perl create tcp connections

2006-12-12 Thread Lawrence Statton XE2/N1GAK

Take a step back -- what are you trying to accomplish?

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g
Computer  software  consists of  only  two  components: ones  and
zeros, in roughly equal proportions.   All that is required is to
sort them into the correct order.

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




reg exp

2006-12-12 Thread Derek B. Smith
I have a string like so:

/home/dbsmith/passwd.oftappp1.hpux and I need to parse
out oftappp1 and hpux.

I have tried to use substr and and regexp with =~.
Here is what I have tried, but need some help cause I
am getting frustrated.

NOTE: strings after passwd are variable in length,
could be 3-10 characters long.

use strict;
use warnings;
my $string = qw(/home/dbsmith/passwd.dubhpr01.sun);
#my ($host_name) = $string =~ /\.\w+\.\w+/g;
my ($host) = substr ($string,21);
my ($OS) = substr ($string,-3);


The OS variable outputs 'sun' but obviously will not
work for hpux or linux.

And the host variable outputs 'dubhpr01.sun' when all
I need is 'dubhpr01' 

thank you
derek

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




Re: reg exp

2006-12-12 Thread D. Bolliger
Derek B. Smith am Dienstag, 12. Dezember 2006 23:19:
> I have a string like so:
>
> /home/dbsmith/passwd.oftappp1.hpux and I need to parse
> out oftappp1 and hpux.
>
> I have tried to use substr and and regexp with =~.
> Here is what I have tried, but need some help cause I
> am getting frustrated.
>
> NOTE: strings after passwd are variable in length,
> could be 3-10 characters long.
>
> use strict;
> use warnings;
> my $string = qw(/home/dbsmith/passwd.dubhpr01.sun);
> #my ($host_name) = $string =~ /\.\w+\.\w+/g;
[snipped]

my ($offtap1, $hpux)='/home/dbsmith/passwd.dubhpr01.sun'=~/\.(\w+)\.(\w+)\Z/;

More general help for all sorts of regex tasks is provided by perldoc perlre.

Dani

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




Re: reg exp

2006-12-12 Thread John W. Krahn
Derek B. Smith wrote:
> I have a string like so:
> 
> /home/dbsmith/passwd.oftappp1.hpux and I need to parse
> out oftappp1 and hpux.
> 
> I have tried to use substr and and regexp with =~.
> Here is what I have tried, but need some help cause I
> am getting frustrated.
> 
> NOTE: strings after passwd are variable in length,
> could be 3-10 characters long.
> 
> use strict;
> use warnings;
> my $string = qw(/home/dbsmith/passwd.dubhpr01.sun);

Assigning a list to a scalar makes no sense.

> #my ($host_name) = $string =~ /\.\w+\.\w+/g;
> my ($host) = substr ($string,21);
> my ($OS) = substr ($string,-3);

$ perl -le'
my $string = q(/home/dbsmith/passwd.dubhpr01.sun);
my ( $host, $OS ) = ( split /\./, $string )[ -2, -1 ];
print for $host, $OS;
'
dubhpr01
sun



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.   -- Larry Wall

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




Re: reg exp

2006-12-12 Thread Lawrence Statton XE2/N1GAK

If you're dealing with variable length strings, separated by some kind
of character, then regexp is the tool you want, not substr.

This snippet will work so long as hostname and platformname are made
up of \w ... if not, substitute in an appropriate character class.

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

foreach my $filename (qw (
   /home/dbsmith/passwd.dubhpr01.sun
   /some/other/path/passwd.fizzbox.hpux
   /yet/another/path/passwd.gronko.aix
   /still/more/paths/to/passwd.foohost.linux
)
 ) {
  my ($hostname, $platform) = $filename =~ m|\.(\w+)\.(\w+)$|;
  print "Hostname: $hostname, Platform: $platform\n";
}


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




Re: reg exp

2006-12-12 Thread I . B .

or just:
my $filename="/home/dbsmith/passwd.duby02.linux";
my ($pass,$hostname,$platform)=split /\./, $filename;

~i


On 12/12/06, Lawrence Statton XE2/N1GAK <[EMAIL PROTECTED]> wrote:



If you're dealing with variable length strings, separated by some kind
of character, then regexp is the tool you want, not substr.

This snippet will work so long as hostname and platformname are made
up of \w ... if not, substitute in an appropriate character class.

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

foreach my $filename (qw (
   /home/dbsmith/passwd.dubhpr01.sun
   /some/other/path/passwd.fizzbox.hpux
   /yet/another/path/passwd.gronko.aix
   /still/more/paths/to/passwd.foohost.linux
)
 ) {
  my ($hostname, $platform) = $filename =~ m|\.(\w+)\.(\w+)$|;
  print "Hostname: $hostname, Platform: $platform\n";
}


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





Re: reg exp

2006-12-12 Thread Derek B. Smith

--- "D. Bolliger" <[EMAIL PROTECTED]> wrote:

> Derek B. Smith am Dienstag, 12. Dezember 2006 23:19:
> > I have a string like so:
> >
> > /home/dbsmith/passwd.oftappp1.hpux and I need to
> parse
> > out oftappp1 and hpux.
> >
> > I have tried to use substr and and regexp with =~.
> > Here is what I have tried, but need some help
> cause I
> > am getting frustrated.
> >
> > NOTE: strings after passwd are variable in length,
> > could be 3-10 characters long.
> >
> > use strict;
> > use warnings;
> > my $string =
> qw(/home/dbsmith/passwd.dubhpr01.sun);
> > #my ($host_name) = $string =~ /\.\w+\.\w+/g;
> [snipped]
> 
> my ($offtap1,
>
$hpux)='/home/dbsmith/passwd.dubhpr01.sun'=~/\.(\w+)\.(\w+)\Z/;
> 
> More general help for all sorts of regex tasks is
> provided by perldoc perlre.
> 
> Dani
> 

Ah...yes I was thinking of using $1 and $2...cant
beleive I forgot.  Thanks though!

And Lawrence thank you for the tip: "If you're dealing
with variable length strings, separated by some kind
of character, then regexp is the tool you want, not
substr."


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




extracting common substrings...

2006-12-12 Thread Helliwell, Kim
Is there a function (perhaps in a library module) that would take two
strings and return the common substring (if any) contained in the
arguments? I've been looking for such a beast on CPAN, but no luck so
far.

 

If not, I guess I have to write it myself...

 

Any help appreciated.

 

Kim Helliwell

LSI Logic Corporation

Work: 408 433 8475

Cell: 408 832 5365

[EMAIL PROTECTED]

 

Please Note: My email address changed to [EMAIL PROTECTED]
  on Oct 14. The old email address
([EMAIL PROTECTED]) will stop working after Jan 15, 2007. Please update
your address book and distribution lists accordingly. Thank you.

 



Re: extracting common substrings...

2006-12-12 Thread Tom Phoenix

On 12/12/06, Helliwell, Kim <[EMAIL PROTECTED]> wrote:


Is there a function (perhaps in a library module) that would take two
strings and return the common substring (if any) contained in the
arguments?


You want the longest possible common substring? Or all of the longest
ones, if there's more than one?

I did something like this once. I used Perl's ability to treat strings
as bitstrings to xor one string against the other, at various
positions. Then I checked each xor result for a string of zeroes (i.e.
character matches) longer than any yet known, recording the length and
location of each match. Once I had checked all possible offsets that
could give a long-enough match, it was a simple matter to extract the
matching substring.

Of course, that method assumed ASCII characters were bytes. If you
might have non-byte characters in your data, this algorithom could
still work, but you'll need to code with more care.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

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




Re: extracting common substrings...

2006-12-12 Thread John W. Krahn
Helliwell, Kim wrote:
> Is there a function (perhaps in a library module) that would take two
> strings and return the common substring (if any) contained in the
> arguments? I've been looking for such a beast on CPAN, but no luck so
> far.

Perhaps this is what you require:

http://search.cpan.org/~jfreeman/Algorithm-LCSS-0.01/LCSS.pm



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.   -- Larry Wall

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




Re: extracting common substrings...

2006-12-12 Thread D. Bolliger
Helliwell, Kim am Dienstag, 12. Dezember 2006 21:56:
> Is there a function (perhaps in a library module) that would take two
> strings and return the common substring (if any) contained in the
> arguments? I've been looking for such a beast on CPAN, but no luck so
> far.
>
>
>
> If not, I guess I have to write it myself...

While I was writing it myself ;-) because I did not find anything via google 
or CPAN - missing term LCSS... John posted Algorithm:LCCS.

I thought I post it anyway instead of copying it to /dev/null.

The script contains a testcase with "long" strings, it takes 1.2 secs on my 
old machine (the test case is certainly not a worst case scenario).

It is just a dirty hack, using a naive aproach, and not proved to work 
correctly.

Here it is, comments are welcome:

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

sub lcss {
  my ($s1, $s2)[EMAIL PROTECTED];

  my $max1=length($s1)-1;
  my $max2=length($s2)-1;

  # make $s1 the shorter string
  #
  ($s1, $s2, $max1, $max2)=($s2, $s1, $max2, $max1)
if $max1 > $max2;

  my %found;
  my $longest=0;

  foreach my $i (0..$max1) {
foreach my $j ($i..$max1) {
  my $searchlen=$j-$i+1;

  next if $searchlen < $longest; # because longest css searched

  my $search=substr($s1, $i, $searchlen); # pattern to search

  $found{$1}++ for ($s2=~/($search)/g); # although count not used below

  # not optimal because no test if match succeeded above
  #
  $longest=$searchlen if defined $1;
}
  }

  # (should) select only one random longest string if several present:
  #
  print '(one) LCSS found: ',
(sort {length($b) <=> length($a)} keys %found)[0], "\n";
}

### Test case:

my $pat=join '', 'hello' x 100;
my $bar=join '', 'hello' x 99;
my $foo=join '', 'a' x 100;

lcss ($pat, $bar.$foo.$bar.$pat.$bar.$foo.$bar);
lcss ('donut', 'I just want to eat one donut please!');
lcss ('I just want to eat one donut please!', 'donut');
__END__

Dani

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




Re: Re: extracting common substrings...

2006-12-12 Thread Tom Phoenix

On 12/12/06, D. Bolliger <[EMAIL PROTECTED]> wrote:


  $found{$1}++ for ($s2=~/($search)/g); # although count not used below


Didn't $search just come from the data? It's a string, not a pattern.
If it's got any metacharacters, it could break your pattern, or worse.

Cheers!

--Tom Phoenix
Stonehenge Perl Training

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




Re: extracting common substrings...

2006-12-12 Thread D. Bolliger
Tom Phoenix am Mittwoch, 13. Dezember 2006 02:32:
> On 12/12/06, D. Bolliger <[EMAIL PROTECTED]> wrote:
> >   $found{$1}++ for ($s2=~/($search)/g); # although count not used
> > below
>
> Didn't $search just come from the data? It's a string, not a pattern.
> If it's got any metacharacters, it could break your pattern, or worse.

Ouch, you're right Tom! I completely omitted (speak: forgot) *any* security 
considerations at this development state :-(

=> $s2=~/(\Q$search\E)/g

Waiting for other comments... good night, sleep time here :-)

Dani

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




Re: extracting common substrings...

2006-12-12 Thread D. Bolliger
D. Bolliger am Mittwoch, 13. Dezember 2006 02:25:

Sorry for answering my own post...

[snipped]
> The script contains a testcase with "long" strings, it takes 1.2 secs on my
> old machine (the test case is certainly not a worst case scenario).
[snipped]
> ### Test case:
>
> my $pat=join '', 'hello' x 100;
> my $bar=join '', 'hello' x 99;
> my $foo=join '', 'a' x 100;
[snipped]

With the following worser case test, 

my $baz=join '', 'hiho'  x1;
my $pat=join '', $baz, 'hello' x 100;
my $bar=join '', $baz, 'hello' x 99;
my $foo=join '', 'a' x 100;

execution time increases to... wait a sec ;-)... still running...

"If there's a module, use the module" ?

Dani




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




Re: Conditional sorting across two arrays

2006-12-12 Thread Mumia W.

On 12/12/2006 04:00 AM, Rinku Mahesh wrote:

  Hi,
   
 I've two arrays of same depth (let it be 6)
   
  1st Array:-  @unique {11 , 23, 44, 66, 900, 1009}

  2nd Array:- @occ_count {2, 77, 22, 2, 77,29}
   
  Here I'm looking for a sorting mechanism with the following conditions:-
   
  a. Sort 2nd Array in descending order such that it should reflect {77,77,29,22,2,2} 
   
  b. The values of 1st array should also change in accordance with the positions of elements of 2nd array such that sorting the 1st array should reflect {23,900,1009,44,11,66}
   
  23 and 900 or 11 and 66 can be interchanged but whatever value is lesser should appear first in the list.
   
  c. As there are repeatations in 2nd Array (77 and 2) there could be two values associated with these elements in 1st array (23/900 or 11/66). Here 23 is lesser than 900 thus it should appear fist in the list.
   
   
  If the above explaination is confusing I'm looking for a way where every element of an array can be mapped to corresponding element of another array and as a whole both the arrays require a sorting w.r.t. 2nd array.
   
   
  Can anyone suggest an eficient way to implement same.
   
  TIA,
   
  Rinku





I hope this is both easy and efficient enough for you:

use strict;
use warnings;
use Sort::Key qw(nkeysort);
my (@unique, @occ_count);
@unique = (66 , 23, 44, 11, 900, 1009);
@occ_count = (2, 77, 22, 2, 77,29);

my @indexes = nkeysort { $occ_count[$_] * 1 + $unique[$_] }
(0..$#occ_count);
@unique = map $unique[$_], @indexes;
@occ_count = map $occ_count[$_], @indexes;
print "@unique\n";
print "@occ_count\n";



The line where @indexes is assigned is probably the most confusing, so 
I'll only explain that one. Sort::Key allows you to sort while 
specifying your key field in an easy and natural manner. Because I want 
to sort two arrays, I have to sort array indexes--not the arrays 
themselves; that's what (0..$#occ_count) does; it creates a list of 
array indexes.


Inside of the { ... }, I specify the key field on which to sort. The 
easy way to do this would be to use { $occ_count[$_] }, but that would 
only sort the array indexes based on what's in @occ_count, and I have to 
take @unique into consideration also, so I make the occ_count part of 
the forumula much more important than the "unique" part of the forumla 
by multiplying $occ_count[$_] by 1; this should put it well above 
the range of numbers in @unique.


Then nkeysort tells Sort::Key to do a numeric sort.


HTH



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




Re: module to calculate time difference between two

2006-12-12 Thread Mumia W.

On 12/12/2006 06:35 AM, Mathew wrote:


Maybe this can get you started:
http://beta.nntp.perl.org/group/perl.datetime/2006/05/msg6342.html

Mathew




What is beta.nntp.perl.org ?



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




Re: module to calculate time difference between two

2006-12-12 Thread Mathew Snyder
Mumia W. wrote:
> On 12/12/2006 06:35 AM, Mathew wrote:
>>
>> Maybe this can get you started:
>> http://beta.nntp.perl.org/group/perl.datetime/2006/05/msg6342.html
>>
>> Mathew
>>
>>
> 
> What is beta.nntp.perl.org ?
> 
> 
> 

I'm it is an archive of perl.org mailing lists.  For instance
http://beta.nntp.perl.org/group/perl.beginners/ is the page for this list.

Mathew

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