[30664] in Perl-Users-Digest
Perl-Users Digest, Issue: 1909 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 9 00:09:43 2008
Date: Wed, 8 Oct 2008 21:09:08 -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: 1909
Today's topics:
($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ??? <kramer@ufl.edu>
Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ? <jimsgibson@gmail.com>
Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ? <tadmc@seesig.invalid>
Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ? <ben@morrow.me.uk>
Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ? <jurgenex@hotmail.com>
Re: <Help> How to use routines from another perl script (fidokomik\)
Re: Date Problem <whynot@pozharski.name>
Re: efficient way to write multiple loops code <nospam-abuse@ilyaz.org>
Re: efficient way to write multiple loops code <tadmc@seesig.invalid>
Re: Need to remove an array from another array <me@privacy.invalid>
Parsing CSV and " " <stpra123@gmail.com>
Re: Parsing CSV and " " <ben@morrow.me.uk>
Re: sort on multiple hash values <someone@example.com>
Re: sysread <dontmewithme@got.it>
Re: sysread <ben@morrow.me.uk>
Re: sysread <dontmewithme@got.it>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 08 Oct 2008 20:16:14 -0400
From: kramer <kramer@ufl.edu>
Subject: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ???
Message-Id: <gcjig4$fg3a$1@usenet.osg.ufl.edu>
I expected ($x == ($y or $z)) to return true if $x equals either $y or
$z. iow to give the same as (($x==$y) or ($x==$z)). But it doesn't.
Why?
------------------------------
Date: Wed, 08 Oct 2008 17:51:39 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ???
Message-Id: <081020081751393200%jimsgibson@gmail.com>
In article <gcjig4$fg3a$1@usenet.osg.ufl.edu>, kramer <kramer@ufl.edu>
wrote:
> I expected ($x == ($y or $z)) to return true if $x equals either $y or
> $z. iow to give the same as (($x==$y) or ($x==$z)). But it doesn't.
>
> Why?
Because Perl is not English. I can't think of any computer language
that will parse '($x == ($y or $z))' as '(($x==$y) or ($x==$z));'.
All computer languages with which I am familiar will respect the
explicit parentheses you have added to that expression and evaluate
'($y or $z)' first, then evaluate the result of that sub-expression in
'($x == result)'.
Perhaps you were thinking of arithmetic:
($x * ($y + $z)) == (($x * $y) + ($x * $z))
or maybe boolean logic:
($x and ($y or $z)) == (($x and $y) or ($x and $z))
--
Jim Gibson
------------------------------
Date: Wed, 8 Oct 2008 19:49:17 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ???
Message-Id: <slrngeql8d.40h.tadmc@tadmc30.sbcglobal.net>
kramer <kramer@ufl.edu> wrote:
> I expected ($x == ($y or $z)) to return true if $x equals either $y or
> $z. iow to give the same as (($x==$y) or ($x==$z)). But it doesn't.
>
> Why?
Because you told it (with parenthesis) to evaluate the "or"
before evaluating the "==".
And if you stop telling it that, ie:
($x == $y or $z)
Then it will evaluate the "$x == $y" followed by evaluating the "or"
(with one of its operands being the boolean result of the "$x == $y").
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Thu, 9 Oct 2008 03:01:39 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ???
Message-Id: <3950s5-sc7.ln1@osiris.mauzo.dyndns.org>
Quoth Jim Gibson <jimsgibson@gmail.com>:
> In article <gcjig4$fg3a$1@usenet.osg.ufl.edu>, kramer <kramer@ufl.edu>
> wrote:
>
> > I expected ($x == ($y or $z)) to return true if $x equals either $y or
> > $z. iow to give the same as (($x==$y) or ($x==$z)). But it doesn't.
> >
> > Why?
>
> Because Perl is not English. I can't think of any computer language
> that will parse '($x == ($y or $z))' as '(($x==$y) or ($x==$z));'.
use Quantum::Superpositions;
my ($x, $y, $z) = (1, 2, 1);
if ($x == any($y, $z)) {
print "YAY!";
}
This will be built in to Perl 6, where (IIRC) you will be able to write
($x == $y|$z)
The concept is called 'junctions'.
Ben
--
It will be seen that the Erwhonians are a meek and long-suffering people,
easily led by the nose, and quick to offer up common sense at the shrine of
logic, when a philosopher convinces them that their institutions are not based
on the strictest morality. [Samuel Butler, paraphrased] ben@morrow.me.uk
------------------------------
Date: Wed, 08 Oct 2008 19:32:45 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ???
Message-Id: <k6qqe4tp8pi1imiktu00vl9a30o58h2lb0@4ax.com>
kramer <kramer@ufl.edu> wrote:
>I expected ($x == ($y or $z)) to return true if $x equals either $y or
>$z. iow to give the same as (($x==$y) or ($x==$z)). But it doesn't.
>Why?
Because even for Boolean values '==' and 'or' are not distributive, let
alone for non-Boolean values which you are probably using.
1 == (0 or 0) ====> 1 == 0 ====> 0
(1 or 0) == (1 or 0) ====> 1 == 1 ====> 1
jue
------------------------------
Date: Thu, 9 Oct 2008 02:33:34 +0200
From: "Petr Vileta \(fidokomik\)" <stoupa@practisoft.cz>
Subject: Re: <Help> How to use routines from another perl script
Message-Id: <gcjjmo$1gu$1@aioe.org>
Tim Greer wrote:
> Notice:
>
> require "/var/myroutines/ex1.pl;
>
Oh so ;-( I must change my glasses I think ;-)
--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail.
Send me your mail from another non-spammer site please.)
Please reply to <petr AT practisoft DOT cz>
------------------------------
Date: Thu, 09 Oct 2008 01:10:38 +0300
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: Date Problem
Message-Id: <unnvr5x9km.ln2@carpet.zombinet>
Graham Stow <graham.stow@gmail.com> wrote:
*SKIP*
> if ($date ne $todays_date) {
> $date= $todays_date;
> do something.....
> }
That "does something" because I<$date> is not equal to I<$todays_date>.
*CUT*
Surely you're interested why condition succedes? To know it you must do
a big deal of cleanup -- insert 2 statements (C<use strict;> and
C<use warnings;>) before any code; then clean your code until perl will
be happy with your attempts; then run. I believe, (dirt guessing here)
sooner or later you'll see warning about something undefined.
p.s. excuse my bad mood, but I think you won't.
--
Torvalds' goal for Linux is very simple: World Domination
------------------------------
Date: Wed, 8 Oct 2008 20:50:47 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: efficient way to write multiple loops code
Message-Id: <gcj6f7$hu8$1@agate.berkeley.edu>
[A complimentary Cc of this posting was sent to
Tad J McClellan
<tadmc@seesig.invalid>], who wrote in article <slrngepo67.von.tadmc@tadmc30.sbcglobal.net>:
> > @file1 = split('\|',$file1_line);
> A pattern match should *look like* a pattern match:
> @file1 = split(/\|/,$file1_line);
In general, I do not agree. A split on a constant string WITHOUT
METACHARS should better be written as a split on string. However, in
this particular case, it is better to use something looking as a REx.
However, do you really find /\|/ very esthetic? ;-) Can it be
written better than m'\|'?
Yours,
Ilya
------------------------------
Date: Wed, 8 Oct 2008 18:27:35 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: efficient way to write multiple loops code
Message-Id: <slrngeqgf7.3ce.tadmc@tadmc30.sbcglobal.net>
Ilya Zakharevich <nospam-abuse@ilyaz.org> wrote:
> [A complimentary Cc of this posting was sent to
> Tad J McClellan
><tadmc@seesig.invalid>], who wrote in article <slrngepo67.von.tadmc@tadmc30.sbcglobal.net>:
>
>> > @file1 = split('\|',$file1_line);
>
>> A pattern match should *look like* a pattern match:
>> @file1 = split(/\|/,$file1_line);
>
> In general, I do not agree. A split on a constant string WITHOUT
> METACHARS should better be written as a split on string.
I like that idea enough that I may actually change my preference...
> However, in
> this particular case, it is better to use something looking as a REx.
>
> However, do you really find /\|/ very esthetic? ;-)
No. In this case, the nature of the beast precludes anything esthetic. :-(
> Can it be
> written better than m'\|'?
That is not too objectionable, though I kinda like /\Q|/
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Wed, 8 Oct 2008 16:42:10 -0500
From: "M.L." <me@privacy.invalid>
Subject: Re: Need to remove an array from another array
Message-Id: <gcj9f9$can$1@registered.motzarella.org>
>>Given an array: @planets = qw(mercury venus earth mars jupiter saturn
>>uranus
>>neptune pluto);
>>and @removes = qw(venus mars pluto);
>>
>>I'd like to know how to grep the @removes from @planets such that
>>@planets = qw(mercury earth jupiter saturn uranus neptune);
>
> Coming to think of it, your spec via example is rather incomplete:
> - what about if @planets contains multiple entries with the same value,
> e.g. 'mars'? Are you supposed to remove all occurences of 'mars' from
> @planets, even if @removes contains only one entry 'mars'? And if only
> one is to be removed, then which one? Any? The first?
> -probably less critical: what about if @removes contain an element that
> is not part of @planets? Error? Or just ignore?
>
> It appears to me you are using the arrays to represent mathematical
> sets: elements are unique and sequence doesn't matter. If this is true,
> then a hash would be a much better data structure.
Nothing really complex about my setup. I created @planets as an easy to
understand analogue representing the unique form variables on my HTML form.
I deliberately created the @removes array of variables as a means to get rid
of any unneeded form variables before they were printed out for an email
response.
For my email response problem it would be necessary to get rid of *any*
duplicate entries in @planets that were specified in @removes since I
wouldn't want the recipient to ever see them. Since I created @removes for a
specific duty, I would never allow it to have duplicate entries. Even if it
did, I've already used Google to learn how to get rid of duplicate entries
from an array. So I'm all set for now.
------------------------------
Date: Wed, 8 Oct 2008 16:58:03 -0700 (PDT)
From: hotkitty <stpra123@gmail.com>
Subject: Parsing CSV and " "
Message-Id: <c7980a90-75f7-4190-a9e5-dacbdf2c04f8@a2g2000prm.googlegroups.com>
I'm trying to parse the following csv file in a linux environment:
"this is row1 column0","this is row1 column1","this is row1
column2","this is row1 column3","this
is row1 column4"
"this is row2 column0","this is row2 column1","this is row2
column2","this is row2 column3","this is
row2 column4"
Pretty standard CSV but with the last column running onto the next
line it gets screwed up somehow as my script doesn't recognize when a
new row starts. I tried substituting the carriage return but still no
luck. When I open up the file on my windows box w/ notepad I get the
following (notice the " " that is added to the end of the
line):
"this is row1 column0","this is row1 column1","this is row1
column2","this is row1 column3","this
is row1 column4"" "
"this is row2 column0","this is row2 column1","this is row2
column2","this is row2 column3","this is
row2 column4"" "
Maybe I'm just overlooking some simple solution but how do I deal w/
the " " as Linux doesn't recognize it?
Thanks in advance. My code is as follows:
my $csv= Text::CSV->new(); ####### I've also tried setting binary to 1
but still didn't work
open (CSV, "<", $thisfile) or die $!;
while (<CSV>) {
if ($csv->parse($_)) {
my @csvcolumns= $csv->fields();
my $newstuff = "$csvcolumns[1]";
open(OUT, ">>$thatfile");
print OUT "$newstuff\n";
close(OUT);
}
------------------------------
Date: Thu, 9 Oct 2008 02:56:06 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Parsing CSV and " "
Message-Id: <mu40s5-sc7.ln1@osiris.mauzo.dyndns.org>
Quoth hotkitty <stpra123@gmail.com>:
> I'm trying to parse the following csv file in a linux environment:
>
> "this is row1 column0","this is row1 column1","this is row1
> column2","this is row1 column3","this
> is row1 column4"
> "this is row2 column0","this is row2 column1","this is row2
> column2","this is row2 column3","this is
> row2 column4"
>
> Pretty standard CSV but with the last column running onto the next
> line it gets screwed up somehow as my script doesn't recognize when a
> new row starts.
How do you know when a new record starts? Can you guarantee that a line
beginning with " is always the start of a new record? If so, then
running the file through something like
perl -lne'if (/^"/) { print $line; $line = "" } $line .= $_;
END { print $line }'
may help. If any of your fields could end with a space (so the
terminating " might wrap onto the next line), or could end up not being
quoted, you might have a problem.
> I tried substituting the carriage return but still no
> luck. When I open up the file on my windows box w/ notepad I get the
> following (notice the " " that is added to the end of the
> line):
> "this is row1 column0","this is row1 column1","this is row1
> column2","this is row1 column3","this
> is row1 column4"" "
> "this is row2 column0","this is row2 column1","this is row2
> column2","this is row2 column3","this is
> row2 column4"" "
>
> Maybe I'm just overlooking some simple solution but how do I deal w/
> the " " as Linux doesn't recognize it?
Where did it come from? How did you transfer the file Linux to Windows:
did you somehow use a web browser or a stupid mail client or something
else that has messed up the file? If " " is never part of
valid data then removing it is as simple as adding
s/" "//;
to the start of the above.
Ben
--
Raise your hand if you're invulnerable.
[ben@morrow.me.uk]
------------------------------
Date: Wed, 08 Oct 2008 11:12:55 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: sort on multiple hash values
Message-Id: <HK6Hk.5628$GT6.4166@newsfe12.iad>
Zhiliang Hu 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:
Have you thought about using a Hash of Arrays instead?
my %hasha = (
Marty => [ 32877, 2 ],
Bart => [ 14857, 2 ],
Torq => [ 65221, 1 ],
);
> foreach $key (sort mysort keys %hasha ) { ... }
Why are you are supplying sort with a subroutine that has sort in it?
Did you want to sort on the keys as well as the values?
foreach my $key ( mysort keys %hasha ) { ... }
> sub mysort {
> map { $_->[0] }
> sort { $a->[0] cmp $b->[0] }
> map { [ $_, split(/::/,[0] ] }
You are supplying split() with an anonymous array which translates to a
number that has no ':' characters in it. The mysort subroutine is
passed a list of keys from the %hasha hash in the @_ array but you are
not accessing that list, but even if you were, to get the value from
that key you need to get it from the hash as $hasha{$_}. You are
comparing what would be the keys of the hash but you said you wanted to
compare the values.
> }
>
> but confused as how to 'map' the elements... need help -- Thanks in
> advance!
sub mysort {
map $_->[ 0 ],
sort { $a->[ 1 ] <=> $b->[ 1 ] }
map [ $_, ( split /::/, $hasha{ $_ } )[ 0 ] ],
@_
}
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
------------------------------
Date: Wed, 08 Oct 2008 21:15:36 +0200
From: Larry <dontmewithme@got.it>
Subject: Re: sysread
Message-Id: <dontmewithme-5A82F2.21153608102008@news.tin.it>
In article <dontmewithme-3EC607.12225608102008@news.tin.it>,
Larry <dontmewithme@got.it> wrote:
> > If you need to use your own buffer, push :unix (or just use sysread) and
> > do it by hand.
I have been using the following to read from STDIN:
use IO::Handle;
$io = new IO::Handle;
if ($io->fdopen(fileno(STDIN),"r")) {
print $io->getline;
$io->close;
}
How can I use :unix with that?
thanks
------------------------------
Date: Wed, 8 Oct 2008 20:26:07 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: sysread
Message-Id: <f3evr5-k37.ln1@osiris.mauzo.dyndns.org>
Quoth Larry <dontmewithme@got.it>:
> In article <dontmewithme-3EC607.12225608102008@news.tin.it>,
> Larry <dontmewithme@got.it> wrote:
>
> > > If you need to use your own buffer, push :unix (or just use sysread) and
> > > do it by hand.
>
> I have been using the following to read from STDIN:
>
> use IO::Handle;
>
> $io = new IO::Handle;
> if ($io->fdopen(fileno(STDIN),"r")) {
binmode $io, ":unix";
> print $io->getline;
> $io->close;
> }
>
> How can I use :unix with that?
...but that almost certainly isn't what you want. Why do you think you
need to do your own buffering in any case? readline (<>,
IO::Handle->getline) depend on perl's buffering to work; while they will
work with a :unix filehandle, it's incredibly inefficient.
What are you actually trying to do?
Ben
--
You poor take courage, you rich take care:
The Earth was made a common treasury for everyone to share
All things in common, all people one.
'We come in peace'---the order came to cut them down. [ben@morrow.me.uk]
------------------------------
Date: Wed, 08 Oct 2008 23:14:01 +0200
From: Larry <dontmewithme@got.it>
Subject: Re: sysread
Message-Id: <dontmewithme-02E557.23140108102008@news.tin.it>
In article <f3evr5-k37.ln1@osiris.mauzo.dyndns.org>,
Ben Morrow <ben@morrow.me.uk> wrote:
> ...but that almost certainly isn't what you want. Why do you think you
> need to do your own buffering in any case? readline (<>,
> IO::Handle->getline) depend on perl's buffering to work; while they will
> work with a :unix filehandle, it's incredibly inefficient.
>
> What are you actually trying to do?
I'm sorry I copied and pasted the code above. Actually what I am trying
to do is:
if ( $io->fdopen(fileno(STDIN),"r") )
{
while($io->read(my $buf, 1024))
{
# ...deal with $buf...
}
$io->close;
}
thanks
------------------------------
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 1909
***************************************