[23519] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 5728 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 30 03:05:48 2003

Date: Thu, 30 Oct 2003 00:05:12 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 30 Oct 2003     Volume: 10 Number: 5728

Today's topics:
    Re: calculate an average (Jay Tilton)
        difference between defined & exists (Palaniappan)
    Re: difference between defined & exists <usenet@morrow.me.uk>
    Re: DOS window. <kalinaubears@iinet.net.au>
    Re: How to generate this list? (Mark Jason Dominus)
        In search of elegant code: inverting a string (David Filmer)
    Re: In search of elegant code: inverting a string (Jay Tilton)
    Re: In search of elegant code: inverting a string (Malcolm Dew-Jones)
    Re: In search of elegant code: inverting a string <mgjv@tradingpost.com.au>
    Re: multiline regular expression, is it possible? (Bill)
    Re: multiline regular expression, is it possible? <flavell@ph.gla.ac.uk>
    Re: multiline regular expression, is it possible? (Bill)
        Parsing results of ps? <marv@stevens.com>
    Re: Parsing results of ps? <nospam@bigpond.com>
    Re: Parsing results of ps? <member46291@dbforums.com>
    Re: Parsing results of ps? <goodcall__1@hotmail.com>
    Re: Singleton process <usenet@morrow.me.uk>
        Sort two dimensional array with multiple keys (Milkweed)
    Re: Sort two dimensional array with multiple keys <usenet@morrow.me.uk>
        sourcing a file from perl <jaws@ericsson.ca>
    Re: sourcing a file from perl <usenet@morrow.me.uk>
    Re: sourcing a file from perl <tony_curtis32@_SPAMTRAP_yahoo.com>
    Re: sourcing a file from perl (Malcolm Dew-Jones)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Wed, 29 Oct 2003 23:11:16 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: calculate an average
Message-Id: <3fa02a7e.88119324@news.erols.com>

"Jack" <jaddo@yahoo.com.> wrote:

: I need to calculate an average of every last-3-values.
:
:  my ($av, $add, @A);
: Foreach my $item ( @data ) {
  ^
  ^ Syntax error

:             If ( $#A == 2) {
              ^
              ^ Another syntax error

:                         $add = o;
                                 ^
                                 ^ Should this be a zero?

:                         shift @A;
:                         push @A, $item
                                        ^
                                        ^ Missing semicolon

:                         foreach my $j ( @A ) { $add += $j };
:             } else {
:                         push @A, $item;
:                         if ( $#A == 2 ) {
:                                     foreach my $j ( @A ) { $add += $j };
:                         }
:             }
:             $av = $add/3;

Missing a right curly bracket here.

: I feel there is a better way or faster code than this

One without as many errors, surely.  :)

There is way too much duplicated code.  Rewriting it into a work-alike
might look like:

    my @A;
    foreach my $item ( @data ) {
        push @A, $item;
        next if @A < 3;
        shift @A if @A > 3;
        my $add = 0;
        $add += $_ for @A;
        my $av = $add/3;
        print "average: $av\n";
    }

Array slicing would be more convenient than pushing/shifting values in a
separate array.  Using the sum() routine from the List::Util module
eliminates even more clutter.

    use List::Util 'sum';
    for(0 .. $#data-2) {
        my $av = sum( @data[$_ .. $_+2] ) / 3;
        print "average: $av\n";
    }



------------------------------

Date: 29 Oct 2003 22:26:31 -0800
From: palam_analog@yahoo.co.in (Palaniappan)
Subject: difference between defined & exists
Message-Id: <a7b604a1.0310292226.59d6325@posting.google.com>

hi all,

What is the difference between 'defined' operator
and 'exists' operator?


Thanks & regards,
palani


------------------------------

Date: Thu, 30 Oct 2003 06:33:37 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: difference between defined & exists
Message-Id: <bnqbc1$7qu$1@wisteria.csv.warwick.ac.uk>


palam_analog@yahoo.co.in (Palaniappan) wrote:
> hi all,
> 
> What is the difference between 'defined' operator
> and 'exists' operator?

my @a;

$a[0] = 1;

# defined $a[0] is true
# exists  $a[0] is true

$a[0] = undef; # or undef $a[0];

# defined $a[0] is false
# exists  $a[0] is true

delete $a[0];

# defined $a[0] is false
# exists  $a[0] is false

defined applies to any value: it tests if that value is 'undef' or
not. exists applies to array or hash members: it tests whether there
is any value, defined or no, there at all.

Ben

-- 
don't get my sympathy hanging out the 15th floor. you've changed the locks 3
times, he still comes reeling though the door, and soon he'll get to you, teach
you how to get to purest hell. you do it to yourself and that's what really
hurts is you do it to yourself just you, you and noone else *  ben@morrow.me.uk


------------------------------

Date: Thu, 30 Oct 2003 11:48:54 +1100
From: Sisyphus <kalinaubears@iinet.net.au>
Subject: Re: DOS window.
Message-Id: <3fa060b4$0$1756$5a62ac22@freenews.iinet.net.au>

Richard S Beckett wrote:
> Guys,
> 
> I've been writing perl scripts at work on a w2k machine, but as I'm now
> doing a personal project, I thought I'd better do it at home.
>

Only if a) you think you might get caught, or b) you're self-employed.

> For various reasons, I'm still running w98 at home, and have discovered that
> the w98 dos window is particularly crap, as I can't specify a buffer size.
> 
> If I put cmd.exe from my w2k machine onto  the w98 machine, will it run OK,
> and give me the dos window I'm used to, or is this a bad idea?
> 

I did that once - iirc it ran fine but didn't provide the full cmd.exe 
functionality. I'm no longer sure of the details. Give it a try and see 
what happens.

MSYS (as already mentioned), Cygwin bash, and 4DOS (which is not free) 
are 3 alternatives that come to mind .... in so far as "if they don't 
provide what you're after, then it aint available".

Cheers,
Rob

-- 
To reply by email u have to take out the u in kalinaubears.



------------------------------

Date: Wed, 29 Oct 2003 23:13:17 +0000 (UTC)
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: How to generate this list?
Message-Id: <bnphid$qge$1@plover.com>


In article <bnor8e$j8o$2@wisteria.csv.warwick.ac.uk>,
Ben Morrow  <usenet@morrow.me.uk> wrote:
>This is of course equivalent to the problem of enumerating the
>rationals, which was solved IIRC by Cantor... :)

In some sense, although I don't think he ever left any compilable code.



------------------------------

Date: 29 Oct 2003 15:50:48 -0800
From: IneverReadAnythingSentToMe@hotmail.com (David Filmer)
Subject: In search of elegant code: inverting a string
Message-Id: <e4c916dd.0310291550.2dd33482@posting.google.com>

OK, a simple task: I want to invert (reverse) a string, such that
   'ABCDEFG'  becomes 'GFEDCBA'

I first thought of:

   print reverse split //, $foo;   # Yikes, did I write THAT?!?!?!

but realized that first thoughts are often FAR more twisted and
convoluted than they ever need to be, so I reconsidered with:

   while ($foo) {
      print chop $foo;   #a new Chinese dish?
   }

which is FAR better than my first idea, but still doesn't strike me as
truly elegant.

I wonder how other hacks might code it.


------------------------------

Date: Thu, 30 Oct 2003 00:08:37 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: In search of elegant code: inverting a string
Message-Id: <3fa0562e.99305514@news.erols.com>

IneverReadAnythingSentToMe@hotmail.com (David Filmer) wrote:

: OK, a simple task: I want to invert (reverse) a string, such that
:    'ABCDEFG'  becomes 'GFEDCBA'
: 
: I first thought of:
: 
:    print reverse split //, $foo;   # Yikes, did I write THAT?!?!?!
: 
: but realized that first thoughts are often FAR more twisted and
: convoluted than they ever need to be, so I reconsidered with:
: 
:    while ($foo) {
:       print chop $foo;   #a new Chinese dish?
:    }
: 
: which is FAR better than my first idea, but still doesn't strike me as
: truly elegant.
: 
: I wonder how other hacks might code it.

They would simply use reverse() in scalar context.

    my $foo = 'ABCDEFG';
    print scalar reverse $foo;



------------------------------

Date: 29 Oct 2003 16:27:21 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: In search of elegant code: inverting a string
Message-Id: <3fa05ae9@news.victoria.tc.ca>

David Filmer (IneverReadAnythingSentToMe@hotmail.com) wrote:
: OK, a simple task: I want to invert (reverse) a string, such that
:    'ABCDEFG'  becomes 'GFEDCBA'

: I first thought of:

:    print reverse split //, $foo;   # Yikes, did I write THAT?!?!?!

: but realized that first thoughts are often FAR more twisted and
: convoluted than they ever need to be, so I reconsidered with:

:    while ($foo) {
:       print chop $foo;   #a new Chinese dish?
:    }

: which is FAR better than my first idea, but still doesn't strike me as
: truly elegant.

: I wonder how other hacks might code it.

I would use reverse $string ;

I am guessing you tried

	print reverse 'ABCDEFG' ;

unfortunately that would be misleading.  Instead you must type

	print scalar reverse 'ABCDEFG';

or

	$reversed = reverse 'ABCDEFG';
	print $reversed;


Print puts things in list context, so reverse reverses your _list_ of
strings, but there's only one string in your list, so reversing your list
made no difference (if you tried what I suspected).



------------------------------

Date: 30 Oct 2003 00:34:40 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: In search of elegant code: inverting a string
Message-Id: <slrnbq0n52.ppp.mgjv@verbruggen.comdyn.com.au>

On 29 Oct 2003 15:50:48 -0800,
	David Filmer <IneverReadAnythingSentToMe@hotmail.com> wrote:
> OK, a simple task: I want to invert (reverse) a string, such that
>    'ABCDEFG'  becomes 'GFEDCBA'
> 
> I first thought of:
> 
>    print reverse split //, $foo;   # Yikes, did I write THAT?!?!?!
> 
> but realized that first thoughts are often FAR more twisted and
> convoluted than they ever need to be, so I reconsidered with:
> 
>    while ($foo) {
>       print chop $foo;   #a new Chinese dish?
>    }

I'd probably do

print scalar reverse $foo;

As the documentation explains, reverse() in scalar context returns a
string.

> which is FAR better than my first idea, but still doesn't strike me as
> truly elegant.
> 
> I wonder how other hacks might code it.

other hacks might code a subroutine like this:

sub my_reverse
{
    my $s = shift;
    substr $s, $_, 1, substr $s, -$_ - 1, 1, substr $s, $_, 1
	for (0 .. (length $s)/2 - 1);
    $s;
}

Martien
-- 
                        | 
Martien Verbruggen      | The four horsemen of the apocalypse are
Trading Post Australia  | called Abort, Retry, Ignore and Fail.
                        | 


------------------------------

Date: 29 Oct 2003 16:29:32 -0800
From: wherrera@lynxview.com (Bill)
Subject: Re: multiline regular expression, is it possible?
Message-Id: <239ce42f.0310291629.6353ad08@posting.google.com>

Ben Morrow <usenet@morrow.me.uk> wrote in message news:<bnou1b$k9a$2@wisteria.csv.warwick.ac.uk>...


> > Is the space between id: and \d accounted for here?
> 
> If you mean 'is it required to be there for the regex to match' then,
> since I didn't use the /x switch, yes, it is.

When parsing HTML pages, one problem I often have is failing to allow
for and match whitespace properly. So much so that it usually pays to
preprocess the whitespace variations out before doing the regex.

I guess that was not the PHP problem though. Never mind :).


------------------------------

Date: Thu, 30 Oct 2003 00:48:49 +0000
From: "Alan J. Flavell" <flavell@ph.gla.ac.uk>
Subject: Re: multiline regular expression, is it possible?
Message-Id: <Pine.LNX.4.53.0310300043460.31128@ppepc56.ph.gla.ac.uk>

On Thu, 29 Oct 2003, Bill wrote:

> When parsing HTML pages, one problem I often have is failing to allow
> for and match whitespace properly.

I dare say that the problem you mention isn't nearly so great as the
fact that a regex is the wrong tool for parsing HTML.

It might be good enough for simplified HTML constructs that you've
carefully controlled yourself, but if you need to handle the full
range of (even) valid HTML that you'd get from other sources, you'd be
scuppered.

(And that's not starting on the truly vast amounts of "Sturgeon's Law
Evidence" that relies almost entirely on error fixup in browsers to
achieve the author's intentions.  But I digress, probably.)


------------------------------

Date: 29 Oct 2003 23:06:47 -0800
From: wherrera@lynxview.com (Bill)
Subject: Re: multiline regular expression, is it possible?
Message-Id: <239ce42f.0310292306.31edcf27@posting.google.com>

"Alan J. Flavell" <flavell@ph.gla.ac.uk> wrote in message news:<Pine.LNX.4.53.0310300043460.31128@ppepc56.ph.gla.ac.uk>...

> It might be good enough for simplified HTML constructs that you've
> carefully controlled yourself, but if you need to handle the full
> range of (even) valid HTML that you'd get from other sources, you'd be
> scuppered.
> 
> (And that's not starting on the truly vast amounts of "Sturgeon's Law
> Evidence" that relies almost entirely on error fixup in browsers to
> achieve the author's intentions.  But I digress, probably.)

Interesting...the last time I came across this was with parsing the
'clit' formatted pages of the ebook-to-html translator by that name
into a multipage, indexed format. I used HTML::TreeBuilder to parse
it, but to teach the script to more or less understand what is in the
book you still have to use regex on the actual tag and text
content--spaces, returns, escaped characters, tabs and all.


------------------------------

Date: Thu, 30 Oct 2003 03:31:53 GMT
From: Marv <marv@stevens.com>
Subject: Parsing results of ps?
Message-Id: <r311qvg4k55msqkpk4v3v50ar35qihekme@4ax.com>

Hello,

I'm a perl beginner and having trouble with what would appear like a
simple task.

I need to capture PID number from each line into a variable while in a
loop. The help I need is how to get to split each line of the output
so that I get the PID number regardless of how many digits long it is.
The output of ps -x gives me the pid number in the first column,
however it seems to be right justified in this column as such:

 2193 ?		S	blahblah blah
26127 pts/0	S	blahblah blah
 4513 ?		S	blahblah blah

So when I try to do a split using a space as the delimiter some of the
variables end up empty.

How could I do this so that I always end up with the PID number?

Thanks,
Marv


------------------------------

Date: Thu, 30 Oct 2003 15:03:52 +1000
From: Gregory Toomey <nospam@bigpond.com>
Subject: Re: Parsing results of ps?
Message-Id: <4757993.kLSjGLBYkO@gregs-web-hosting-and-pickle-farming>

It was a dark and stormy night, and Marv managed to scribble:

> Hello,
> 
> I'm a perl beginner and having trouble with what would appear like a
> simple task.
> 
> I need to capture PID number from each line into a variable while in a
> loop. The help I need is how to get to split each line of the output
> so that I get the PID number regardless of how many digits long it is.
> The output of ps -x gives me the pid number in the first column,
> however it seems to be right justified in this column as such:
> 
>  2193 ?               S       blahblah blah
> 26127 pts/0   S       blahblah blah
>  4513 ?               S       blahblah blah
> 
> So when I try to do a split using a space as the delimiter some of the
> variables end up empty.
> 
> How could I do this so that I always end up with the PID number?
> 
> Thanks,
> Marv

Here's a snippet of code (based on linux ps) that gives you a start:

my($pid,$tname,$etime,$cmd,$emin,$ehour,$background);                                                                         
for (split '\n', qx(ps -u $> -o pid,tname,etime,cmd --no-headers)) {
  #unpack ps line 
  ($pid, $tname, $etime,$cmd) = unpack "a5 x a11 x a8 x a200",$_;
  # decode a few fields
  $etime =~ /((\d*):)?(\d*):(\d*)$/;
  $ehour  = $2;
  $emin   = $3;
  # is this process running in the background
  $background=/\?/;
  #etc 
}


gtoomey      


------------------------------

Date: Wed, 29 Oct 2003 23:47:36 -0500
From: ko_ <member46291@dbforums.com>
Subject: Re: Parsing results of ps?
Message-Id: <3539451.1067489256@dbforums.com>


Originally posted by Marv 

> Hello,

>

> I'm a perl beginner and having trouble with what would appear like a

> simple task.

>

> I need to capture PID number from each line into a variable while in a

> loop. The help I need is how to get to split each line of the output

> so that I get the PID number regardless of how many digits long it is.

> The output of ps -x gives me the pid number in the first column,

> however it seems to be right justified in this column as such:

>

>  2193 ?               S       blahblah blah

> 26127 pts/0   S       blahblah blah

>  4513 ?               S       blahblah blah

>

> So when I try to do a split using a space as the delimiter some of the

> variables end up empty.

>

> How could I do this so that I always end up with the PID number?

>

> Thanks,

> Marv 



If you split $_ without using a pattern leading whitespace is ignored,
so you can do something like this:



#!/usr/bin/perl -w

use strict;



my @arr = <DATA>;



foreach (@arr) {

  my($pid) = split;

  print "$pid\n" if defined $pid;

}



__DATA__

 2193 ?         S       blahblah blah

26127 pts/0     S       blahblah blah

 4513 ?         S       blahblah blah



This is explained in 'perldoc -f split'



HTH - keith


--
Posted via http://dbforums.com


------------------------------

Date: Thu, 30 Oct 2003 05:47:38 GMT
From: "Jack D." <goodcall__1@hotmail.com>
Subject: Re: Parsing results of ps?
Message-Id: <_B1ob.1667$EY3.647@edtnps84>

"Marv" <marv@stevens.com> wrote in message
news:r311qvg4k55msqkpk4v3v50ar35qihekme@4ax.com...
> Hello,
>
> I'm a perl beginner and having trouble with what would appear like a
> simple task.
>
> I need to capture PID number from each line into a variable while in a
> loop. The help I need is how to get to split each line of the output
> so that I get the PID number regardless of how many digits long it is.
> The output of ps -x gives me the pid number in the first column,
> however it seems to be right justified in this column as such:
>
>  2193 ? S blahblah blah
> 26127 pts/0 S blahblah blah
>  4513 ? S blahblah blah
>
> So when I try to do a split using a space as the delimiter some of the
> variables end up empty.
>
> How could I do this so that I always end up with the PID number?

I have had great success with:

http://search.cpan.org/~durist/Proc-ProcessTable-0.39/

It does the splitting for you!

Jack




------------------------------

Date: Wed, 29 Oct 2003 23:17:15 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Singleton process
Message-Id: <bnphpr$sbi$2@wisteria.csv.warwick.ac.uk>


rjohnson@shell.com (Roy Johnson) wrote:
> mjd@plover.com (Mark Jason Dominus) wrote in message
> news:<bnopno$7e7$1@plover.com>...
> > Can I suggest the following delightful and foolproof solution to this
> > problem?
> > 
> >         http://perl.plover.com/yak/flock/samples/slide006.html
> 
> For some reason, both suggestions (using $0 and using DATA) fail on my
> Unix box. I am using flock, though, and when I create a throwaway file
> for the purpose, it works properly.

Out of interest: which Unix are you on, what error do you get, and
does your perl use flock() or fcntl() locking (if you can tell)?

Ben

-- 
I've seen things you people wouldn't believe: attack ships on fire off the
shoulder of Orion; I've watched C-beams glitter in the darkness near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die.  |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-|  ben@morrow.me.uk


------------------------------

Date: 29 Oct 2003 15:05:14 -0800
From: chris.mielke@drake.edu (Milkweed)
Subject: Sort two dimensional array with multiple keys
Message-Id: <f0f3e49.0310291505.51ebe4b7@posting.google.com>

I am trying sort an multi-dimensional array with three elements in
each "row".

Here's a look at a small section of input data:

207.28.198.86 10.36.1.121 0310291642
207.28.198.86 10.36.1.121 0310291753
207.28.201.113 10.77.2.241 0310291642
207.28.200.113 10.75.2.87 0310291642
207.28.199.86 10.76.1.80 0310291642
207.28.198.104 10.1.3.153 0310291642
207.28.198.86 10.36.1.121 0310291324
207.28.199.104 10.2.2.77 0310291642
207.28.195.33 10.2.4.111 0310291642

These are timestamped NAT translations from a firewall.
What I want to do is sort this data by element 0 (global IP) as the
primary key and element 2 (timestamp) as the secondary key so I can
keep a historical record of NAT translations for a given global IP.

I can't seem to get the sort to work. Here is a copy of what I have
tried so far:

#!/usr/bin/perl
use strict;
use warnings;
use Net::Telnet::Cisco;
use Data::Sorting qw( :basics :arrays );

 ...

foreach (@xlate) { # @xlate is populated by a SNMP call to the
firewall
    next unless /^Global/;
    my ($global, $local) = (split /\s/)[1,3];
    push @entries, [$global,$local,$timestamp]; 
# each row of @entries now looks like the sample data above
}

#Sort entries by IP (primary key) and date (secondary key) and print
output
my @ordered = sorted_array( @entries,
    { engine=> 'packed', sortkey=>$_[0][0] },
    sortkey=>$_[0][2] );
#my $i = 0;
#keys my %h = @history;
#@h{ sort map pack('C4 A x N' => $->[0] =~
/(\d+)\.(\d+)\.(\d+)\.(\d+)/,
#    $_->[2], $i++) => @history } = @history;
#my @ordered = @h{ sort keys %h };
open NEW, ">xlate-ordered"
    or die "Could not open xlate-ordered for writing: $!\n";
for my $row (@ordered) {
    print NEW "@$row\n";
}

As you can see I have tried using the "indexed sort" method from "A
Fresh Look at Efficient Perl Sorting" as well as the Data::Sorting
module with no success. Help with either option would be much
appreciated!

Thanks,
Chris


------------------------------

Date: Wed, 29 Oct 2003 23:29:41 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Sort two dimensional array with multiple keys
Message-Id: <bnpih5$sth$1@wisteria.csv.warwick.ac.uk>


chris.mielke@drake.edu (Milkweed) wrote:
> I am trying sort an multi-dimensional array with three elements in
> each "row".
> 
> Here's a look at a small section of input data:
> 
> 207.28.198.86 10.36.1.121 0310291642
> 207.28.198.86 10.36.1.121 0310291753
> 207.28.201.113 10.77.2.241 0310291642
> 207.28.200.113 10.75.2.87 0310291642
> 207.28.199.86 10.76.1.80 0310291642
> 207.28.198.104 10.1.3.153 0310291642
> 207.28.198.86 10.36.1.121 0310291324
> 207.28.199.104 10.2.2.77 0310291642
> 207.28.195.33 10.2.4.111 0310291642
> 
> These are timestamped NAT translations from a firewall.
> What I want to do is sort this data by element 0 (global IP) as the
> primary key and element 2 (timestamp) as the secondary key so I can
> keep a historical record of NAT translations for a given global IP.
> 
> I can't seem to get the sort to work. Here is a copy of what I have
> tried so far:
> 
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Net::Telnet::Cisco;
> use Data::Sorting qw( :basics :arrays );
> 
> ...
> 
> foreach (@xlate) { # @xlate is populated by a SNMP call to the
> firewall
>     next unless /^Global/;
>     my ($global, $local) = (split /\s/)[1,3];
>     push @entries, [$global,$local,$timestamp]; 
> # each row of @entries now looks like the sample data above
> }

Try this (the technique is known as the Schwartzian Transform after
Randal Schwartz):

my @sorted = map  { $_->[0] }
             sort { $b->[1] cmp $a->[1] }
             map  { [ $_, $_->[0].$_->[2] ] }
             @entries;

This will sort in ASCIIbetical order, first of global IP and then of
timestamp. If you want the IPs in a better order you could use
             map { [ $_, inet_aton($_->[0]).$_->[2] ] }
for the first map instead (after using Socket, obviously).

I found out about this from http://perl.plover.com/: a very useful
resource.

Ben

-- 
"The Earth is degenerating these days. Bribery and corruption abound.
Children no longer mind their parents, every man wants to write a book,
and it is evident that the end of the world is fast approaching."
     -Assyrian stone tablet, c.2800 BC                         ben@morrow.me.uk


------------------------------

Date: Wed, 29 Oct 2003 18:14:19 -0500
From: JAWS <jaws@ericsson.ca>
Subject: sourcing a file from perl
Message-Id: <3FA049CB.CA79AED1@ericsson.ca>

I need to use the csh source command or anything equivalent form a perl
script but i can't access the source command because the shell fired by
perl is sh.  Ideally, i also need to be able to use the new environment
variables in the rest of my perl program...

thanks in advance for suggestions

jp



------------------------------

Date: Wed, 29 Oct 2003 23:35:43 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: sourcing a file from perl
Message-Id: <bnpisf$sth$2@wisteria.csv.warwick.ac.uk>

JAWS <jaws@ericsson.ca> wrote:
> I need to use the csh source command or anything equivalent form a perl
> script but i can't access the source command because the shell fired by
> perl is sh.  Ideally, i also need to be able to use the new environment
> variables in the rest of my perl program...

In general what you want to do can't be done: csh and Perl are
different languages, and perl does not understand csh. It's the same
as if you tried to source a (complicated) sh script from in csh: it
just wouldn't work.

Your best bet is to read the file yourself in the Perl program, and
extract the variable definitions out of it. If it is a complicated
script (ie. the variable settings are not easy to parse) then you may
be better off writing a little csh script which sources the file and
prints out the variables, and then opening a pipe to that script (see
perldoc -f open, the bit about opening a name ending in '|') and
parsing the results instead.

Ben

-- 
  Joy and Woe are woven fine,
  A Clothing for the Soul divine       William Blake
  Under every grief and pine          'Auguries of Innocence'
  Runs a joy with silken twine.                                ben@morrow.me.uk


------------------------------

Date: Wed, 29 Oct 2003 17:38:15 -0600
From: Tony Curtis <tony_curtis32@_SPAMTRAP_yahoo.com>
Subject: Re: sourcing a file from perl
Message-Id: <87u15ripi0.fsf@limey.hpcc.uh.edu>

>> On Wed, 29 Oct 2003 18:14:19 -0500,
>> JAWS <jaws@ericsson.ca> said:

> I need to use the csh source command or anything
> equivalent form a perl script but i can't access the
> source command because the shell fired by perl is sh.
> Ideally, i also need to be able to use the new
> environment variables in the rest of my perl program...

Usually the only sane solution to this is to wrap the perl
program in a csh script that sources the (sub-)script
first.

If the source'd script is just a few setenv's though then
maybe you could just read it in as data and parse it into
$ENV assignments.

hth
t


------------------------------

Date: 29 Oct 2003 16:06:11 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: sourcing a file from perl
Message-Id: <3fa055f3@news.victoria.tc.ca>

JAWS (jaws@ericsson.ca) wrote:
: I need to use the csh source command or anything equivalent form a perl
: script but i can't access the source command because the shell fired by
: perl is sh.  

So use that shell to fire up csh to source the file.

Ideally, i also need to be able to use the new environment
: variables in the rest of my perl program...

The truth comes out, it's a faq, you can't do what you want directly (on
unix like systems).

o You can define the vars before you run perl.  This is by far the easiest 
in the long run.

	$ . vars.sh
	$ perl my_script.pl

o From within perl you check for the variables, and if they are not set
then use system to run a short shell script that a) sets the variables and
then b) (re)invokes your perl script

	# just an illustration !!

	if not $ENV{FOOBAR}
	{ exit system(". stuff; export FOOBAR=1; perl $0 @ARGV");
	}

o You can ask the shell script to print the variables and then parse the
output

	# just an illustration

	#!/bin/sh
	# vars.sh
	MY_VAR="set its value";

	#!perl
	$values = `. vars.sh; echo MY_VAR=$MY_VAR`;
	my ($my_var)=$values=~m/MY_VAR=(.*)/;

o You can parse the shell script yourself, it's easy for simply X=Y
assignments.

	#!perl
	# YOU MUST TRUST THE CONFIG FILE (of course you do or you couldn't 
	# source it in the shell script either)
	use strict;
	my ($MY_VAR);

	{   local @ARGV=qw(vars.sh);
	#   local $SIG{__WARN__}=sub{1}; # do we want to see warnings.
	    map {eval "\$$_"} grep {/^\w+=/}<>;
	}

o There are probably modules that will do variations of the above to pull 
configurations variables out of configuration files.


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 5728
***************************************


home help back first fref pref prev next nref lref last post