[30663] in Perl-Users-Digest
Perl-Users Digest, Issue: 1908 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 8 14:09:47 2008
Date: Wed, 8 Oct 2008 11:09:10 -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 Wed, 8 Oct 2008 Volume: 11 Number: 1908
Today's topics:
Comparing audio files <kieranocall@gmail.com>
Re: Comparing audio files <jurgenex@hotmail.com>
Date Problem <graham.stow@gmail.com>
Re: Date Problem <tadmc@seesig.invalid>
Re: efficient way to write multiple loops code <hirenshah.05@gmail.com>
Re: efficient way to write multiple loops code xhoster@gmail.com
Re: efficient way to write multiple loops code <tadmc@seesig.invalid>
Re: efficient way to write multiple loops code <hirenshah.05@gmail.com>
Re: expect.pm - stdout buffering issue <yandry77@gmail.com>
Re: How to escape single quotes inside fields but not t <hjp-usenet2@hjp.at>
sort on multiple hash values <zhilianghu@gmail.com>
Re: sort on multiple hash values <jurgenex@hotmail.com>
Re: sysread <dontmewithme@got.it>
Re: update XML file with perl or other...? <mirod@xmltwig.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 8 Oct 2008 07:39:32 -0700 (PDT)
From: kieran <kieranocall@gmail.com>
Subject: Comparing audio files
Message-Id: <9e2e825c-fa63-4734-9584-996d5ca7db59@r38g2000prr.googlegroups.com>
Hello,
I am trying to compare two similar audio files (WAV). From what i have
read i need to sample both audio files at certain frequencies and run
these through a FFT and then compare the results. Can anyone advise me
if this is the correct approach and also describe the steps i need to
take to get to the stage where I can compare the files.
TIA,
Kieran
------------------------------
Date: Wed, 08 Oct 2008 09:12:43 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Comparing audio files
Message-Id: <0vmpe4pp5l3meisgs22o54i6i8pecc31fq@4ax.com>
kieran <kieranocall@gmail.com> wrote:
>I am trying to compare two similar audio files (WAV). From what i have
>read i need to sample both audio files at certain frequencies and run
>these through a FFT and then compare the results. Can anyone advise me
>if this is the correct approach and also describe the steps i need to
>take to get to the stage where I can compare the files.
You could try the ESP::PSI module.
jue
------------------------------
Date: Wed, 8 Oct 2008 17:04:56 +0100
From: "Graham Stow" <graham.stow@gmail.com>
Subject: Date Problem
Message-Id: <gcilkc$1ulc$1@energise.enta.net>
The following script (or something much like it) runs continuously on a web
server and does something when the date changes (i.e. at the bewitching
hour). Problem is, it does it 2 or 3 times, when I want it to do it only
once. Any ideas anyone?
$start_date = "2008-09-20"; # this 'date' (string) will never change
and could be anything
&get_todays_date;
$date = $todays_date;
while ($date ne $start_date) { # the 2 'dates' will never equate, so
this script runs forever
&get_todays_date;
if ($date ne $todays_date) {
$date= $todays_date;
do something.....
}
}
sub get_todays_date {
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year=$year+1900;
$mon=$mon+1;
if (length($mon) eq 1) {
$mon = "0"."$mon";
}
if (length($mday) eq 1) {
$mday = "0"."$mday";
}
$todays_date = "$year"."-"."$mon"."-"."$mday";
}
------------------------------
Date: Wed, 8 Oct 2008 11:26:19 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Date Problem
Message-Id: <slrngepnpb.von.tadmc@tadmc30.sbcglobal.net>
Graham Stow <graham.stow@gmail.com> wrote:
> The following script (or something much like it) runs continuously on a web
> server
That is likely to waste a boatload of cycles.
It would probably be better to run it once a minute or so using
whatever job scheduling your OS provides, such as "cron".
> and does something when the date changes (i.e. at the bewitching
> hour). Problem is, it does it 2 or 3 times, when I want it to do it only
> once. Any ideas anyone?
I do not see how it is happening more than once...
> $start_date = "2008-09-20"; # this 'date' (string) will never change
> and could be anything
> &get_todays_date;
You should not use an ampersand on subroutine calls unless you know
what it does, and what it does is what you want to do (it seldom is).
get_todays_date();
> $date = $todays_date;
Communication via global variables is a horrible idea. You should instead
communicate via subroutine arguments and return values:
$todays_date = get_todays_date();
> while ($date ne $start_date) { # the 2 'dates' will never equate, so
> this script runs forever
Why take that chance? If you want a truly infinite loop (which I don't
think you really need here), then
while ( 1 ) {
or even
while ( 'infinite' ) {
would be better.
> &get_todays_date;
$todays_date = get_todays_date();
> if ($date ne $todays_date) {
> $date= $todays_date;
> do something.....
> }
> }
>
> sub get_todays_date {
> ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
> $year=$year+1900;
> $mon=$mon+1;
> if (length($mon) eq 1) {
> $mon = "0"."$mon";
^^^^^^
perldoc -q vars
What's wrong with always quoting "$vars"?
So make that:
$mon = '0'. $mon;
or
$mon = "0$mon";
(but using printf/sprintf is Much Better than trying to do it yourself.)
> }
> if (length($mday) eq 1) {
> $mday = "0"."$mday";
> }
> $todays_date = "$year"."-"."$mon"."-"."$mday";
> }
sub get_todays_date {
my($day, $mon, $year) = (localtime)[3,4,5];
return sprintf '%04d-%02d-%02d', $year+1900, $mon+1, $day;
}
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Wed, 8 Oct 2008 08:08:38 -0700 (PDT)
From: "friend.05@gmail.com" <hirenshah.05@gmail.com>
Subject: Re: efficient way to write multiple loops code
Message-Id: <8e04fe6a-dd2a-4440-a62a-3341227f7d74@r37g2000prr.googlegroups.com>
On Oct 8, 4:18=A0am, s...@netherlands.com wrote:
> On Tue, 07 Oct 2008 22:20:06 GMT, s...@netherlands.com wrote:
> >On Tue, 7 Oct 2008 13:31:50 -0700 (PDT), "friend...@gmail.com" <hirensha=
h...@gmail.com> wrote:
>
> >>Hi,
>
> >>I am trying to analyze some data. I have big data files.
>
> >>I have 3 different files in following format. ($file_1, $file_2,
> >>$file_3)
>
> >>ID | Time | IP | Code
>
> >>Following is psuedo code which I am writing. I want to know another
> >>efficient way to do same thing.
>
> >>open(INFO_1,$file_1);
> >>open(INFO_2,$file_2);
> >>open(INFO_3,$file_3);
>
> >>@file1_lines =3D <INFO_1>;
> >>@file2_lines =3D <INFO_2>;
> >>@file3_lines =3D <INFO_3>;
>
> >>foreach $file1_line (@file1_lines)
> >>{
> >> =A0 =A0 =A0 =A0 @file1 =3D split('\|',$file1_line);
>
> >> =A0 =A0 =A0 =A0 #some code
>
> >> =A0 =A0 =A0 =A0 foreach $file2_line (@file2_lines)
> >> =A0 =A0 =A0 =A0 {
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 @file2 =3D split('\|',$file2_line);
>
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #some code
>
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #if condition between File1 data and F=
ile2 data
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
>
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0#some code
>
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0foreach $file3_=
line (@file3_lines)
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0{
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 @file3 =3D split('\|',$file3_line);
>
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 #some code
>
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0#if condition
>
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}
>
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
>
> >> =A0 =A0 =A0 =A0 }
>
> >>}
>
> >>So I am going thorugh each data of file 1 and depending on if data is
> >>present in file2 and again depending on some if condition I look for
> >>that data in file3.
>
> >>So each data of file1 will have to go through each data of file2 and
> >>each data of file2 will have to go thorugh file3.
>
> >>So this code is taking lot of time. I want some suggestion for
> >>efficient code.
>
> >>Can I use Hash Array (by reading file in =A0hash array)
>
> >Nobody knows the impact of any pseudo code, or what data that
> >it process is. There is no generalization to be sought.
>
> >The best you can do, through trial and error, is benchmark
> >it yourself:
>
> >use Benchmark ':hireswallclock';
> >my $t0 =3D new Benchmark;
>
> >{{{{ code block}}}
>
> >my $t1 =3D new Benchmark;
> >my $tdif =3D timediff($t1, $t0);
> >print STDERR "the code took:",timestr($tdif),"\n";
>
> >sln
>
> Well, if it were my code, I would know exactly how to do it without bench=
marks.
> But you don't know yourself it seams. Do you?
> Instead, you post phoney PSEUDO code as if you know something, which you =
don't.
> Yet put the burdon on the sucker who is stupid enough to respond to you.
>
> Outta here... ignant
>
> sln- Hide quoted text -
>
> - Show quoted text -
Thanks to all for replying.
Can I use Hash even if I don't have unique key ? Because in my data I
need IP and Code which are not necessary to be unique.
Below is my code:
open(INFO_1,$file_1);
open(INFO_2,$file_2);
open(INFO_3,$file_3);
@file1_lines =3D <INFO_1>;
@file2_lines =3D <INFO_2>;
@file3_lines =3D <INFO_3>;
foreach $file1_line (@file1_lines)
{
@file1 =3D split('\|',$file1_line);
$file1_ip =3D $file[2];
$file2_code =3D $file[3];
foreach $file2_line (@file2_lines)
{
@file2 =3D split('\|',$file2_line);
$file2_ip =3D $file[2];
$file2_code =3D $file[3];
if($file1_ip eq $file2_ip)
{
$flag =3D 1;
if($file1_code eq $file2_code)
{
$r_flag =3D 0;
foreach $file3_line (@file3_lines)
{
@file3 =3D split('\|',$file3_line);
$file3_ip =3D $file[2];
$file3_code =3D $file[3];
if(($file1_ip eq $file3_ip) &&
($file1_code eq $file3_code))
{
#some flag
}
}
#depending on flag I increment some counter
}
}
}
#depending on flag I increment some counter
}
------------------------------
Date: 08 Oct 2008 15:24:04 GMT
From: xhoster@gmail.com
Subject: Re: efficient way to write multiple loops code
Message-Id: <20081008112405.849$xG@newsreader.com>
"friend.05@gmail.com" <hirenshah.05@gmail.com> wrote:
>
> Thanks to all for replying.
>
> Can I use Hash even if I don't have unique key ?
Yes and no. Hashes only have unique keys, but the hash value for that
key can be an array or a hash, so effectively each key can have several
values.
> Because in my data I
> need IP and Code which are not necessary to be unique.
In what data structure are they not necessarily unique?
>
> if($file1_ip eq $file2_ip)
> {
> $flag = 1;
$flag is not used elsewhere
> if($file1_code eq $file2_code)
Since this if has no else, and has nothing done between its end and
the end of the previous if, they can be combined into one line
if($file1_ip eq $file2_ip and $file1_code eq $file2_code) {
> {
> $r_flag = 0;
>
> foreach $file3_line (@file3_lines)
> {
> @file3 = split('\|',$file3_line);
> $file3_ip = $file[2];
> $file3_code = $file[3];
>
> if(($file1_ip eq $file3_ip) &&
> ($file1_code eq $file3_code))
> {
> #some flag
Again with the pseudo-code? Let's say that that means "$r_flag=1;"
Since any execution of that beyond the first is meaningless, that mean
it doesn't matter if the same ip|code shows up more than once in
@file3_lines, so @file3_lines can be reduced to a simple hash instead
> }
>
> }
> #depending on flag I increment some counter
Incrementing a counter of course is count-sensitive, unlike setting a flag.
So it might matter of the same ip|code shows up mutliple times in
@file2_lines. But that could probably be solved by storing the count
in the hash.
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Wed, 8 Oct 2008 11:33:11 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: efficient way to write multiple loops code
Message-Id: <slrngepo67.von.tadmc@tadmc30.sbcglobal.net>
friend.05@gmail.com <hirenshah.05@gmail.com> wrote:
> open(INFO_1,$file_1);
You should always, yes *always*, check the return value from open():
open(INFO_1, $file_1) or die "could not open '$file_1' $!";
Even better, you should use the 3-arg form of open() and a lexical filehandle:
open my $INFO_1, '<', $file_1 or die "could not open '$file_1' $!";
> @file1_lines = <INFO_1>;
@file1_lines = <$INFO_1>; # use the lexical filehandle
> @file1 = split('\|',$file1_line);
A pattern match should *look like* a pattern match:
@file1 = split(/\|/,$file1_line);
> $file1_ip = $file[2];
> $file2_code = $file[3];
You can replace those 3 lines of code with 1 line using a
List Slice (see the "Slices" section in perldata.pod):
my($file1_ip, $file2_code) = (split /\|/, $file1_line)[2,3];
> $flag = 1;
You should choose meaningful variable names.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Wed, 8 Oct 2008 10:05:07 -0700 (PDT)
From: "friend.05@gmail.com" <hirenshah.05@gmail.com>
Subject: Re: efficient way to write multiple loops code
Message-Id: <9f89cf12-ca39-47e8-9060-08c12da6cc4b@n1g2000prb.googlegroups.com>
On Oct 8, 12:33=A0pm, Tad J McClellan <ta...@seesig.invalid> wrote:
> friend...@gmail.com <hirenshah...@gmail.com> wrote:
> > open(INFO_1,$file_1);
>
> You should always, yes *always*, check the return value from open():
>
> =A0 =A0open(INFO_1, $file_1) or die "could not open '$file_1' $!";
>
> Even better, you should use the 3-arg form of open() and a lexical fileha=
ndle:
>
> =A0 =A0open my $INFO_1, '<', $file_1 or die "could not open '$file_1' $!"=
;
>
> > @file1_lines =3D <INFO_1>;
>
> =A0 =A0 @file1_lines =3D <$INFO_1>; # use the lexical filehandle
>
> > =A0 =A0 =A0 =A0 =A0@file1 =3D split('\|',$file1_line);
>
> A pattern match should *look like* a pattern match:
>
> =A0 =A0@file1 =3D split(/\|/,$file1_line);
>
> > =A0 =A0 =A0 =A0 =A0$file1_ip =3D $file[2];
> > =A0 =A0 =A0 =A0 =A0$file2_code =3D $file[3];
>
> You can replace those 3 lines of code with 1 line using a
> List Slice (see the "Slices" section in perldata.pod):
>
> =A0 =A0my($file1_ip, $file2_code) =3D (split /\|/, $file1_line)[2,3];
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$flag =3D 1;
>
> You should choose meaningful variable names.
>
> --
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Hey Tad,
Thanks for your help.
Can you also suggest some efficient way.
Since I am processing three files in loop. It is taking lot of time.
------------------------------
Date: Wed, 8 Oct 2008 08:25:37 -0700 (PDT)
From: Andry <yandry77@gmail.com>
Subject: Re: expect.pm - stdout buffering issue
Message-Id: <f17076d6-658a-40da-be4d-619074f715d7@a18g2000pra.googlegroups.com>
On 26 Set, 15:57, Josef Moellers <josef.moell...@fujitsu-siemens.com>
wrote:
> Andry wrote:
> > On Sep 25, 6:38 pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
> > wrote:
> >> Andry wrote:
>
> >> [...]
>
> >>> Nevertheless, the 'sleep' commands still look out of sync. I'm
> >>> starting thinking that also some delayed answers from the ssh server
> >> No and there's no such thing as an ssh server.
>
> >> You're script is basically doing:
>
> >> print "Running ls\n";
> >> #send()
> >> my $out =3D `ls`;
>
> >> print "sleeping for 5\n";
> >> sleep 5;
>
> >> #expect(...)
> >> print "the output of ls: $out\n" if $out;
>
> >> Expect will run the command, then you tell it what you
> >> expect to happen and/or when that command is finished,
> >> usually by testing if the prompt is being displayed.
> >> At that point your command has finished running and
> >> you can get the output by using before(), or other
> >> methods. =A0Then you can sleep, or send another command
> >> and wait for the prompt. Rinse and repeat...
>
> > Hi guys,
> > Here is a better explanation of what I mentioned yesterday.
> > Based on all your previous suggestions and some intuitive attempts, I
> > found a way to get the script display the STDOUT correctly and log all
> > the output to the log_file, text-log.txt (playing with expect/sleep
> > commands). I also added a couple of partial logs ("ls -l" output into
> > text1-log.txt and "ls" output into text2-log.txt), teeing STDOUT.
> > Notice that, amazingly, if I remove any of the sleep commands I get
> > the wrong output collected by text1-log.txt/text2-log.txt.
> > Here is the script (please, read more after the script text below):
> > *************************************************************
>
> [ code removed ]
>
>
>
> > *************************************************************
> > The problem is that: if you run the same script ten times, only 5 out
> > of 10 times you get the proper output captured by the proper partial
> > log file (either text1-log.txt or text2-log.txt).
> > The rest of the times you get the wrong output logged into text1-
> > log.txt/text2-log.txt: when this happens, usually text1-log.txt
> > contains only the few first lines, whereas text2-log.txt contains the
> > rest of "ls -l" output instead (and not "ls" output!).
> > The log_file=3Dtext-log.txt is always filled with all the expected
> > output instead.
>
> > Since the problem comes out randomly I suspect that the SSH command
> > response can affect the STDOUT I/O operations. Otherwise I don't see a
> > reason why the script should give different results when run multiple
> > times.
>
> > Any thought about the above explained issues?
>
> > Btw, J.Gleixner, do you think I get some better result using
> > exp_before() as you mentioned?
>
> I tried this and I don't see any differences in the files other than
> modification times and/or sizes of files affected by the logins.
>
> Very strange,
>
> Josef
> --
> These are my personal views and not those of Fujitsu Siemens Computers!
> Josef M=F6llers (Pinguinpfleger bei FSC)
> =A0 =A0 =A0 =A0 If failure had no penalty success would not be a prize (T=
. =A0Pratchett)
> Company Details:http://www.fujitsu-siemens.com/imprint.html
Hi all,
After changing the SSH server station, the problem of random
inaccuracy of collected lines from STDOUT has gone away.
So, it seems that Expect for Perl is really sensitive to possible
"jitter" of response messages from a remote terminal (SSH/Telnet
server).
Besides, when building the final script with several repeated
operations like the ones described in this discussion, I had to tune/
tick the correct collection of lines (to guarantee proper output at
the right time) putting multiple expect/send couples (doing dummy
actions, like a newline character...) before each "close STDOUT"
command in order to flush STDOUT as expected. The correct sequence of
such operations was achieved with an empirical method (trying and
trying...).
So, in my opinion, as far as I could experience, Expect for Perl does
not perform very well at all...
Generally speaking, I'd rather use the usual TCL/Expect.
Thanks to all for your support during this discussion,
Andrea
------------------------------
Date: Wed, 8 Oct 2008 18:10:28 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: How to escape single quotes inside fields but not the ones around fields?
Message-Id: <slrngepmrl.vml.hjp-usenet2@hrunkner.hjp.at>
On 2008-10-07 15:08, Ben Morrow <ben@morrow.me.uk> wrote:
>
> Quoth "Henry J." <tank209209@yahoo.com>:
>>
>> OK, one problem pops up trying to use placeholders. We are using DB2
>> which supports insert statements with multiple value tuples, like
>> this:
>>
>> insert into myTable values(v1, v2, v3), (v4, v5, v6), (v7, v8,
>> v9), ....
>>
>> In our lib, such an intert statement is being built on the fly based
>> on a parameter specifiying the number of value tuples (usually
>> 1,000). The multiple values speed up the inserts a lot.
>>
>> Now in order to use placeholders, I need to build SQL like
>>
>>
>> insert into myTable values(?, ?, ?), (?, ?, ?), (?, ?, ?), ....
>> (up to 1,000 tuples)
>>
>> and then bind the values into the 3,000 placeholders. Not sure about
>> the performance impact. I may run some tests. But sharing of any
>> experience on this would be appreciated.
>
> You may find that using placeholders makes the performance problem go
> away. You only need to prepare the statement once (with the placeholders
> in), and that is usually what takes the time. Then you can execute it
> 1,000 times with different bind values, which should be relatively fast.
If DBD::DB2 has proper support for execute_array, you don't even have to
execute it 1000 times - you can execute it once with the data for all
thousand rows. At least for Oracle this is a lot faster than doing lots
of single row inserts.
hp
------------------------------
Date: Wed, 8 Oct 2008 10:36:14 -0700 (PDT)
From: Zhiliang Hu <zhilianghu@gmail.com>
Subject: sort on multiple hash values
Message-Id: <a2120c73-aa51-447f-916b-78dbb182a6b3@a2g2000prm.googlegroups.com>
I have a hash with multiple values, like in:
Marty => 32877::2
Bart => 14857::2
Torq => 65221::1
etc
and I wish to sort on. say, first field of the hash value:
foreach $key (sort mysort keys %hasha ) { ... }
sub mysort {
map { $_->[0] }
sort { $a->[0] cmp $b->[0] }
map { [ $_, split(/::/,[0] ] }
}
but confused as how to 'map' the elements... need help -- Thanks in
advance!
------------------------------
Date: Wed, 08 Oct 2008 11:04:46 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: sort on multiple hash values
Message-Id: <dbtpe4posjamgk8cse33q5ai4qtdghdkdi@4ax.com>
Zhiliang Hu <zhilianghu@gmail.com> wrote:
>I have a hash with multiple values, like in:
>
> Marty => 32877::2
> Bart => 14857::2
> Torq => 65221::1
> etc
>
>and I wish to sort on. say, first field of the hash value:
You don't because hashes by definition do not have a sequence and
therefore cannot be sorted.
What _do_ you want to sort? An array of the keys of the hash? An arrray
of the values of the hash? Something totally different?
See also the FAQ: "How do I sort a hash[...]?"
jue
------------------------------
Date: Wed, 08 Oct 2008 12:22:56 +0200
From: Larry <dontmewithme@got.it>
Subject: Re: sysread
Message-Id: <dontmewithme-3EC607.12225608102008@news.tin.it>
In article <rk3tr5-nhn.ln1@osiris.mauzo.dyndns.org>,
Ben Morrow <ben@morrow.me.uk> wrote:
> If you need to use your own buffer, push :unix (or just use sysread) and
> do it by hand.
Can it actually be done? I've been wondering how to do it for a long time
------------------------------
Date: Wed, 08 Oct 2008 13:24:02 +0200
From: mirod <mirod@xmltwig.com>
Subject: Re: update XML file with perl or other...?
Message-Id: <48ec9840$0$14490$5fc30a8@news.tiscali.it>
inetquestion wrote:
> On Oct 6, 10:25 pm, inetquestion <inetquest...@hotmail.com> wrote:
>> I have an XML document which I would like to modify based on the
>> results of a test. A subset of information contained in the XML file
>> is shown below. If a 'test' to each server:port were to fail, then
>> the xml file should be modified such that the attribute 'off' is set
>> to a value of 1. I was thinking of doing this in perl, but would
>> like to get some suggestions based on ease of modifying files, etc...
>> I've written some basic perl scripts to read an xml file before, but
>> ran into some confusion when trying to write them back out. Any
>> suggestions or pointers?
You could use XML::Twig. Build an XPath-like expression that matches
the element you want to update and when you reach it update the attribute.
The twig_roots/twig_print_outside_roots combo ensures that everything else gets
outputed untouched:
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
# clever argument passing ;--)
my $xml = shift( @ARGV) || 'inet.xml';
my $app = shift( @ARGV) || 'hokiepokie';
my $host = shift( @ARGV) || 'server02';
my $port = shift( @ARGV) || '8081';
# the XPath expression that matches the element to update
my $trigger= qq{app[\@name="$app"]/instance[\@host="$host" and \@port="$port"]};
XML::Twig->new( twig_roots => {$trigger => \&switch_state, },
twig_print_outside_roots => 1, # everything else is untouched
)
->parsefile_inplace( $xml); # not really inplace, just looks like it
sub switch_state
{ my( $t, $instance)= @_;
$instance->set_att( Off => 1); # instance is the element object
$instance->print;
}
--
mirod
------------------------------
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 V11 Issue 1908
***************************************