[23003] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5223 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jul 14 18:10:38 2003

Date: Mon, 14 Jul 2003 15:10:12 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 14 Jul 2003     Volume: 10 Number: 5223

Today's topics:
        testing for array or scalar in a hash. <fredboard@reglage.ath.cx>
    Re: testing for array or scalar in a hash. <noreply@gunnar.cc>
    Re: testing for array or scalar in a hash. (Greg Bacon)
    Re: testing for array or scalar in a hash. <krahnj@acm.org>
    Re: testing for array or scalar in a hash. <usenet@dwall.fastmail.fm>
    Re: testing for array or scalar in a hash. <mbudash@sonic.net>
        TOO SLOW:     `echo ${LINE} | sed -n 's/^.* proto //p'  (secheese)
    Re: TOO SLOW:     `echo ${LINE} | sed -n 's/^.* proto / (rakesh sharma)
    Re: TOO SLOW:     `echo ${LINE} | sed -n 's/^.* proto / (James E Keenan)
    Re: TOO SLOW:     `echo ${LINE} | sed -n 's/^.* proto / (Carlton Brown)
    Re: TOO SLOW:     `echo ${LINE} | sed -n 's/^.* proto / <ben_altman@deadspam.com>
    Re: TOO SLOW:     `echo ${LINE} | sed -n 's/^.* proto / <uri@stemsystems.com>
        Translation with PERL? (Jon)
    Re: Translation with PERL? <asu1@c-o-r-n-e-l-l.edu>
    Re: Wanted - push/rexec/pull script for remote program  (John Ramsden)
        Win32-OLE where's the info? <spikey-wan@bigfoot.com>
    Re: Win32-OLE where's the info? <asu1@c-o-r-n-e-l-l.edu>
    Re: Win32-OLE where's the info? <marks.pryorSHRUB@CHENEYverizon.net>
        XML attributes and values (Saya)
    Re: XML attributes and values <abigail@abigail.nl>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 14 Jul 2003 19:42:02 GMT
From: Fred S <fredboard@reglage.ath.cx>
Subject: testing for array or scalar in a hash.
Message-Id: <pan.2003.07.14.19.41.48.561164@reglage.ath.cx>

Hi, 
I have a hash containing, data, everything is generated on the fly so 
my hash, has some scalars, and some arrays, in it.
How can I test wether a value of the hash is an array and then loop
through it ?
i did the following loop:
it will print the arrays but will skip the scalars, 		

foreach (keys %Options){
    print $_." => \n";
    foreach my $val (@{$Options{$_}}){
        print "\t".$val;
        print "\n";
    }
}

Thanx for the help.


Fred


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

Date: Mon, 14 Jul 2003 22:27:51 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: testing for array or scalar in a hash.
Message-Id: <bev3me$9empi$1@ID-184292.news.uni-berlin.de>

Fred S wrote:
> I have a hash containing, data, everything is generated on the fly
> so my hash, has some scalars, and some arrays, in it.
> How can I test wether a value of the hash is an array and then loop
> through it ?

How about:

     foreach (keys %Options){
         if (ref $Options{$_} eq 'ARRAY') {
             print ...

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



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

Date: Mon, 14 Jul 2003 20:34:06 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: testing for array or scalar in a hash.
Message-Id: <vh64tusirsjt49@corp.supernews.com>

In article <pan.2003.07.14.19.41.48.561164@reglage.ath.cx>,
    Fred S  <fredboard@reglage.ath.cx> wrote:

: I have a hash containing, data, everything is generated on the fly so 
: my hash, has some scalars, and some arrays, in it.
: How can I test wether a value of the hash is an array and then loop
: through it ?
: [snip code]

Use Perl's ref operator to see whether you have an array reference:

    #! /usr/local/bin/perl

    use warnings;
    use strict;

    my %Options = (
        scalar   => 42,
        arrayref => [ qw/ apples oranges bananas / ],
    );

    for (keys %Options) {
        print $_, " => \n",
              map "\t$_\n",
                  ref $Options{$_} ? @{ $Options{$_} } : $Options{$_};
    }

This generates the following output:

    scalar =>
            42
    arrayref =>
            apples
            oranges
            bananas

You could pare the line lengths by iterating with the each operator.

Hope this helps,
Greg
-- 
The more corrupt the state, the more numerous the laws.
    -- Tacitus 


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

Date: Mon, 14 Jul 2003 20:35:00 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: testing for array or scalar in a hash.
Message-Id: <3F131434.72C85006@acm.org>

Fred S wrote:
> 
> I have a hash containing, data, everything is generated on the fly so
> my hash, has some scalars, and some arrays, in it.
> How can I test wether a value of the hash is an array and then loop
> through it ?
> i did the following loop:
> it will print the arrays but will skip the scalars,
> 
> foreach (keys %Options){
>     print $_." => \n";
>     foreach my $val (@{$Options{$_}}){
>         print "\t".$val;
>         print "\n";
>     }
> }

Use the ref operator.

for ( keys %Options ) {
    print $_." => \n";
    if ( ref eq 'ARRAY' ) {
        print "\t", join( "\t", @{$Options{$_}} ), "\n";
    }
}


perldoc -f ref


John
-- 
use Perl;
program
fulfillment


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

Date: Mon, 14 Jul 2003 20:36:33 -0000
From: "David K. Wall" <usenet@dwall.fastmail.fm>
Subject: Re: testing for array or scalar in a hash.
Message-Id: <Xns93B8A8F67EBA7dkwwashere@216.168.3.30>

Fred S <fredboard@reglage.ath.cx> wrote:

> I have a hash containing, data, everything is generated on the fly
> so my hash, has some scalars, and some arrays, in it.
> How can I test wether a value of the hash is an array and then
> loop through it ?

perldoc -f ref

Example:

    use strict;
    use warnings;
    
    my %h = (
        a => [1,2,3,4],
        h => {a=>1,b=>2,c=>3},
        s => 'string'
    );
    
    foreach my $k (sort keys %h) {
        print "$k -> ", ref $h{$k}, "\n";
    }

Note that the output for $h{s} is blank, because $h{s} is a scalar, 
not a reference to a scalar.


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

Date: Mon, 14 Jul 2003 20:38:06 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: testing for array or scalar in a hash.
Message-Id: <mbudash-921A98.13380514072003@typhoon.sonic.net>

In article <pan.2003.07.14.19.41.48.561164@reglage.ath.cx>,
 Fred S <fredboard@reglage.ath.cx> wrote:

> Hi, 
> I have a hash containing, data, everything is generated on the fly so 
> my hash, has some scalars, and some arrays, in it.

prolly not arrays, but array refs, no?

> How can I test wether a value of the hash is an array and then loop
> through it ?
> i did the following loop:
> it will print the arrays but will skip the scalars, 		
> 
> foreach (keys %Options){
>     print $_." => \n";
>     foreach my $val (@{$Options{$_}}){
>         print "\t".$val;
>         print "\n";
>     }
> }
> 
> Thanx for the help.
> 
> 
> Fred

perldoc -f ref

-- 
Michael Budash


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

Date: Mon, 14 Jul 2003 14:50:59 GMT
From: secheese@hotmail.com (secheese)
Subject: TOO SLOW:     `echo ${LINE} | sed -n 's/^.* proto //p' | cut -f1 -d" "`
Message-Id: <3f12c11e.46273617@news.nbnet.nb.ca>

I have a script that monitors a firewall drop log file and I need to
pull the protocol fields.  I used to know exactly where this field
was, so I could easily get the field with this statement:

	PROTOCOL=`awk 'print $5'`

But now the logs are dynamic and the field can be anywhere.  One thing
I do know is that the protocol field always follows a field labelled
"proto".  Thus the follow command gets it for me:

	PROTOCOL=`echo ${LINE} | sed -n 's/^.* proto //p' | cut -f1 -d" "`

Trouble is, this command takes about 10 times as long to run as the
awk did.  The result is that the execution time for my script overall
has gone from about 1 minute to 10 minutes.

Can anyone think of a faster way to get the job done?  BTW, perl is
available, but I'm unfamiliar with the language.

Thanks.



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

Date: 14 Jul 2003 11:38:41 -0700
From: sharma__r@hotmail.com (rakesh sharma)
Subject: Re: TOO SLOW:     `echo ${LINE} | sed -n 's/^.* proto //p' | cut -f1 -d" "`
Message-Id: <ed24e4cf.0307141038.5eb4e3cb@posting.google.com>

secheese@hotmail.com (secheese) wrote in message news:<3f12c11e.46273617@news.nbnet.nb.ca>...
> I have a script that monitors a firewall drop log file and I need to
> pull the protocol fields.  I used to know exactly where this field
> was, so I could easily get the field with this statement:
> 
> 	PROTOCOL=`awk 'print $5'`
> 
> But now the logs are dynamic and the field can be anywhere.  One thing
> I do know is that the protocol field always follows a field labelled
> "proto".  Thus the follow command gets it for me:
> 
> 	PROTOCOL=`echo ${LINE} | sed -n 's/^.* proto //p' | cut -f1 -d" "`
> 
> Trouble is, this command takes about 10 times as long to run as the
> awk did.  The result is that the execution time for my script overall
> has gone from about 1 minute to 10 minutes.
> 
> Can anyone think of a faster way to get the job done?  BTW, perl is
> available, but I'm unfamiliar with the language.

I don't think the slowdown is because of sed, it's probably due to the $LINE
variable(I guess u r using the shell to loop thru the file).

what u can do is:

  sed -ne '/ proto /s/^.* proto  *\([^ ]*\).*$/\1/p' firewall_logfile

or with perl:

perl -wlane '/\sproto\s+\S/&&do{
                shift @F until $F[0] eq "proto";
                print $F[1];
        }
' firewall_logfile


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

Date: 14 Jul 2003 13:00:31 -0700
From: jkeen@concentric.net (James E Keenan)
Subject: Re: TOO SLOW:     `echo ${LINE} | sed -n 's/^.* proto //p' | cut -f1 -d" "`
Message-Id: <b955da04.0307141200.5d1d06a9@posting.google.com>

secheese@hotmail.com (secheese) wrote in message news:<3f12c11e.46273617@news.nbnet.nb.ca>...
> I have a script that monitors a firewall drop log file and I need to
> pull the protocol fields.  I used to know exactly where this field
> was, so I could easily get the field with this statement:
> 
> 	PROTOCOL=`awk 'print $5'`
> 
> But now the logs are dynamic and the field can be anywhere.  One thing
> I do know is that the protocol field always follows a field labelled
> "proto".  Thus the follow command gets it for me:
> 
> 	PROTOCOL=`echo ${LINE} | sed -n 's/^.* proto //p' | cut -f1 -d" "`
> 
# untested:  requires Perl 5.8 or separate installation of Tie::File
module from CPAN

use strict;
use warnings;
use Tie::File;

my $file = 'firewall_log.txt'; # arbitrary; substitute your own
my (@array);
tie @array, 'Tie::File', $file or die "Couldn't tie to $file: $!";
for (my $i=0; $i<=$#array; $i++) {
    if ($array[$i] =~ /^proto/ and $array[$i+1] =~ /^PROTOCOL/) {
        print $array[$i+1];
    }
}
untie @array;


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

Date: 14 Jul 2003 13:58:18 -0700
From: carltonbrown@hotmail.com (Carlton Brown)
Subject: Re: TOO SLOW:     `echo ${LINE} | sed -n 's/^.* proto //p' | cut -f1 -d" "`
Message-Id: <aa611a32.0307141258.4d56686b@posting.google.com>

secheese@hotmail.com (secheese) wrote in message news:<3f12c11e.46273617@news.nbnet.nb.ca>...
> I have a script that monitors a firewall drop log file and I need to
> pull the protocol fields.  I used to know exactly where this field
> was, so I could easily get the field with this statement:
> 
> 	PROTOCOL=`awk 'print $5'`
> 
> But now the logs are dynamic and the field can be anywhere.  One thing
> I do know is that the protocol field always follows a field labelled
> "proto".  Thus the follow command gets it for me:
> 
> 	PROTOCOL=`echo ${LINE} | sed -n 's/^.* proto //p' | cut -f1 -d" "`

while (<INPUTFILE>) {
     @pname = $_ =~ /proto\s+(\w+)/;
     print "I found a protocol named: $pname[0]\n";
}

This works if you've correctly specified your file handles.  That,
along with any customizations that you may consider asking about next,
are intentionally left blank as an opportunity for self-study.


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

Date: Mon, 14 Jul 2003 10:22:53 -0500
From: Ben <ben_altman@deadspam.com>
Subject: Re: TOO SLOW:     `echo ${LINE} | sed -n 's/^.* proto //p' | cut -f1 -d" "`
Message-Id: <beuhsd$9bot1$1@ID-121117.news.uni-berlin.de>

secheese wrote:
> 
> 	PROTOCOL=`echo ${LINE} | sed -n 's/^.* proto //p' | cut -f1 -d" "`
> 
> Trouble is, this command takes about 10 times as long to run as the

 From a POSIX type shell:
    PROTOCOL=${LINE#* proto } # chop everything up to 'proto'
    PROTOCOL=${PROTOCOL%% *}  # chop everything down to first field

regards,
Ben

-- 
BTW. I can be contacted at Username:newsgroup4.replies.benaltw 
Domain:xoxy.net



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

Date: Mon, 14 Jul 2003 21:25:41 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: TOO SLOW:     `echo ${LINE} | sed -n 's/^.* proto //p' | cut -f1 -d" "`
Message-Id: <x77k6k94xe.fsf@mail.sysarch.com>

>>>>> "CB" == Carlton Brown <carltonbrown@hotmail.com> writes:

  >> 
  >> PROTOCOL=`echo ${LINE} | sed -n 's/^.* proto //p' | cut -f1 -d" "`

  CB> while (<INPUTFILE>) {
  CB>      @pname = $_ =~ /proto\s+(\w+)/;
  CB>      print "I found a protocol named: $pname[0]\n";
  CB> }

perl -ne '/proto\s+(\w+)/ && print "I found a protocol named: $1\n"'

using multiple shell commands and progs for this is slow. and that
should be faster then the full perl loop for several reasons (no block
entry, no temp array)

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: 14 Jul 2003 14:37:34 -0700
From: jmckinley@netbotz.com (Jon)
Subject: Translation with PERL?
Message-Id: <43e4cdd3.0307141337.18259da7@posting.google.com>

I've been using Perl for the past two years on my companies website.
I've also enlisted Embeded perl (mod_perl) which is very, very cool!

I've been tasked with redesigning the site again and I must keep
translation in mind. I've been involved before with translating a
website that ran off JAVA, jsp pages and webshere. The translation was
done with property files holding each language as well as image urls.
My questions is can something like this be done with perl and apache.
Can a property file be just a text file that is referenced for
different languages? I'm just hoping some of you out there can give me
a little advice before I start this project. Any ideas or help are
greatly appreciated.

Jon


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

Date: 14 Jul 2003 21:57:39 GMT
From: "A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu>
Subject: Re: Translation with PERL?
Message-Id: <Xns93B8B6B6740A3asu1cornelledu@132.236.56.8>

jmckinley@netbotz.com (Jon) wrote in news:43e4cdd3.0307141337.18259da7
@posting.google.com:

> I've been using Perl for the past two years on my companies website.
> I've also enlisted Embeded perl (mod_perl) which is very, very cool!
> 
> I've been tasked with redesigning the site again and I must keep
> translation in mind. I've been involved before with translating a
> website that ran off JAVA, jsp pages and webshere. The translation was
> done with property files holding each language as well as image urls.
> My questions is can something like this be done with perl and apache.
> Can a property file be just a text file that is referenced for
> different languages? 

If you had been using ResourceBundles backed by property files, take a 
look at the Config::Properties module:

http://search.cpan.org/author/SALVA/Config-Properties-0.51/Properties.pm

Sinan.

-- 
A. Sinan Unur
asu1@c-o-r-n-e-l-l.edu
Remove dashes for address
Spam bait: mailto:uce@ftc.gov


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

Date: 14 Jul 2003 09:31:17 -0700
From: john_ramsden@sagitta-ps.com (John Ramsden)
Subject: Re: Wanted - push/rexec/pull script for remote program builds
Message-Id: <d27434e.0307140145.3bb9f368@posting.google.com>

inwap@inwap.com (Joe Smith) wrote in message news:<_XSPa.352$603.19125@iad-read.news.verio.net>...
> In article <d27434e.0307100917.35665a0c@posting.google.com>,
> John Ramsden <john_ramsden@sagitta-ps.com> wrote:
> >Each time I amend the source I must manually FTP the updated file[s]
> 
>   rsync -auv -e ssh work_dir/ remotehost:work_dir/
> 
> >to a couple of remote systems, compile it on each of these these,
> 
>   ssh remotehost 'cd work_dir; make'
> 
> >and FTP the resulting runfiles back to the development system.
> 
>   rsync -auv -e ssh remotehost:work_dir/ work_dir/

Many thanks for your reply Joe. The only thing I couldn't find,
using 'man rsync' and 'man rsyncd.conf', was info on how or if
rsync converts text file line endings when transferring files
between Windows (or Mac OS) and Unix.

Mind you, if it copies all files verbatim, I can work round this
by using on the target Unix system a script that runs unix2dos
or equivalent on text files before building the run files from
them.

In any case, these days most compilers and suchlike, including
hopefully ActiveState perlapp, are fairly forgiving about dodgy
line endings; so maybe I can just leave the Windows text files
unchanged.

Apologies this has drifted away from Perl.



Cheers

John R Ramsden  (john_ramsden@sagitta-ps.com)


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

Date: Mon, 14 Jul 2003 16:41:42 +0100
From: "Richard S Beckett" <spikey-wan@bigfoot.com>
Subject: Win32-OLE where's the info?
Message-Id: <beuj1v$6nc$1@newshost.mot.com>

Guys,

There's some cool, but complicated stuff that can be done with Win322::OLE
and Excel.

Where does one look for syntax on what can be done?

For example, I've only ever used SaveAs, but this time, I simply want to
save the spreadsheet with the same name as I opened it with, so I just want
to use Save, but I must have the syntax wrong, as it won't work.

Where should I look to find this info?

Thanks.

R.




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

Date: 14 Jul 2003 17:51:36 GMT
From: "A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu>
Subject: Re: Win32-OLE where's the info?
Message-Id: <Xns93B88CFF8377Casu1cornelledu@132.236.56.8>

"Richard S Beckett" <spikey-wan@bigfoot.com> wrote in
news:beuj1v$6nc$1@newshost.mot.com: 

> Guys,
> 
> There's some cool, but complicated stuff that can be done with
> Win322::OLE and Excel.
> 
> Where does one look for syntax on what can be done?
> 
> For example, I've only ever used SaveAs, but this time, I simply want
> to save the spreadsheet with the same name as I opened it with, so I
> just want to use Save, but I must have the syntax wrong, as it won't
> work. 
> 
> Where should I look to find this info?


See 

http://aspn.activestate.com/ASPN/Perl/Products/ActivePerl/site/lib/Win32/
OLE.html

For properties, methods etc. go to 

Tools -> Macro -> Visiual Basic Editor

in Excel.

An example:

http://www.people.cornell.edu/pages/asu1/notes/perl-excel.html

Sinan.

-- 
A. Sinan Unur
asu1@c-o-r-n-e-l-l.edu
Remove dashes for address
Spam bait: mailto:uce@ftc.gov


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

Date: Mon, 14 Jul 2003 21:10:24 GMT
From: "Mark S Pryor" <marks.pryorSHRUB@CHENEYverizon.net>
Subject: Re: Win32-OLE where's the info?
Message-Id: <4%EQa.1066$OC4.551@nwrddc01.gnilink.net>


"Richard S Beckett" <spikey-wan@bigfoot.com> wrote in message
news:beuj1v$6nc$1@newshost.mot.com...
> Guys,
>
> There's some cool, but complicated stuff that can be done with Win322::OLE
> and Excel.
>
> Where does one look for syntax on what can be done?
>
> For example, I've only ever used SaveAs, but this time, I simply want to
> save the spreadsheet with the same name as I opened it with, so I just
want
> to use Save, but I must have the syntax wrong, as it won't work.
>
> Where should I look to find this info?
>
> Thanks.
>
> R.
>

Try my stand alone applet.
TLViewer: turbo Object Browser for scripting
http://mysite.verizon.net/res1ur2j/tlviewer.htm

I even include the Perl creation code for the selected
object.

good luck,
Mark Pryor




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

Date: 14 Jul 2003 10:35:08 -0700
From: vahu@novonordisk.com (Saya)
Subject: XML attributes and values
Message-Id: <9e9517bf.0307140434.3320d3d0@posting.google.com>

Hi, 

Is there someone who cn help me with the following: 

<data> 

<tuple name="Abb">ARG</tuple> 

<tuple name="Reg">EU</tuple> 

</data> 

assuming a XML document as above with alot of data tags containing
tuple tags. I want to find all tuples that have a name value equals to
"Reg" and extract the tags value (eg. EU). How do I achieve that in
Perl.


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

Date: 14 Jul 2003 20:31:15 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: XML attributes and values
Message-Id: <slrnbh64oj.n86.abigail@alexandra.abigail.nl>

Saya (vahu@novonordisk.com) wrote on MMMDCIV September MCMXCIII in
<URL:news:9e9517bf.0307140434.3320d3d0@posting.google.com>:
//  Hi, 
//  
//  Is there someone who cn help me with the following: 
//  
// <data> 
//  
// <tuple name="Abb">ARG</tuple> 
//  
// <tuple name="Reg">EU</tuple> 
//  
// </data> 
//  
//  assuming a XML document as above with alot of data tags containing
//  tuple tags. I want to find all tuples that have a name value equals to
//  "Reg" and extract the tags value (eg. EU). How do I achieve that in
//  Perl.


There are a gazillion XML modules on CPAN. Assuming you have already
tried them, please indicate why they aren't working for you, or otherwise,
people will point you to the XML modules on CPAN.



Abigail
-- 
perl -swleprint -- -_='Just another Perl Hacker'


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

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 5223
***************************************


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