[22944] in Perl-Users-Digest
Perl-Users Digest, Issue: 5164 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jul 1 18:07:51 2003
Date: Tue, 1 Jul 2003 15:05:13 -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 Tue, 1 Jul 2003 Volume: 10 Number: 5164
Today's topics:
Automatic page forwarding in cgi perl script (Max)
Re: Automatic page forwarding in cgi perl script <asu1@c-o-r-n-e-l-l.edu>
Re: Delete array element from inside function <krahnj@acm.org>
Re: Devel::Coverage (James E Keenan)
Re: funny printf behaviour in need of explanation (Dov Levenglick)
Re: funny printf behaviour in need of explanation (James E Keenan)
Re: How do you sort a 2D array with column headers? (Mark Jason Dominus)
Re: Not following "if" <Snygg@gmx.net>
Re: OLE Variant <brian_helterline@hp.com>
Re: OLE Variant <ken.brown@dialwrap.co.uk>
Re: Problem with tk and displaying on the screen <brian_helterline@hp.com>
Re: Q: detect non-existent hash entries w/function? (Daniel Friedman)
Re: Q: detect non-existent hash entries w/function? <uri@stemsystems.com>
Re: Q: detect non-existent hash entries w/function? (Tad McClellan)
Splitting text from a database table <colin@murorum.demon.co.uk>
Re: Splitting text from a database table (Greg Bacon)
Re: Splitting text from a database table <flavell@mail.cern.ch>
Re: Using undef as an array subscript <mpapec@yahoo.com>
Re: Using undef as an array subscript <emschwar@pobox.com>
Re: Using undef as an array subscript (Greg Bacon)
Re: Using undef as an array subscript (Greg Bacon)
Re: Using undef as an array subscript (Greg Bacon)
Re: Using undef as an array subscript <jaspax@u.washington.edu>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 1 Jul 2003 13:16:14 -0700
From: maximuszen@optonline.net (Max)
Subject: Automatic page forwarding in cgi perl script
Message-Id: <3a9c1232.0307011216.d004957@posting.google.com>
print ("Location: http://localhost/ElectronicMedicalRecord/OrderEntry/Orders.html\n\n");
Why won't this work?
------------------------------
Date: 1 Jul 2003 21:54:43 GMT
From: "A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu>
Subject: Re: Automatic page forwarding in cgi perl script
Message-Id: <Xns93ABB63653289asu1cornelledu@132.236.56.8>
maximuszen@optonline.net (Max) wrote in
news:3a9c1232.0307011216.d004957@posting.google.com:
> print ("Location:
> http://localhost/ElectronicMedicalRecord/OrderEntry/Orders.html\n\n");
>
> Why won't this work?
Questions related to CGI but not Perl are better asked elsewhere, for
example comp.infosystems.www.authoring.cgi.
"Won't work" gives no information about the nature of your problem.
Have you looked at the topic "A reference to another document" in section
"How do I send my document back to the client?" available at:
http://hoohoo.ncsa.uiuc.edu/cgi/primer.html
What makes you think every user who runs this CGI program has a web
server running, and also has the document
/ElectronicMedicalRecord/OrderEntry/Orders.html
exists on that machine?
Sinan.
--
A. Sinan Unur
asu1@c-o-r-n-e-l-l.edu
Remove dashes for address
Spam bait: mailto:uce@ftc.gov
------------------------------
Date: Tue, 01 Jul 2003 20:14:40 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Delete array element from inside function
Message-Id: <3F01EBAE.B518285E@acm.org>
JS Bangs wrote:
>
> I've got a function that would like to be able to delete an array element
> from an array in the caller's scope, given only the array element (not the
> array itself, or a reference to it). The idea is to have something that
> duplicates the functionality of delete(), but which tacks a bit of
> other processing onto it. The existing code is quite simple, and looks
> like this:
>
> ##########
>
> sub delete_seg {
> return undef unless is_segment($_[0]);
> $_[0]->clear;
> delete($_[0]);
> }
>
> #########
>
> In the calling script (which has imported the function delete_seg()):
>
> #########
>
> delete_seg($word[3]);
>
> #########
>
> Unfortunately, this does not work. $word[3] remains firmly in place with a
> defined value. What can I do to make this work?
It looks like you need to use splice:
splice @word, 3, 1;
John
--
use Perl;
program
fulfillment
------------------------------
Date: 1 Jul 2003 12:04:33 -0700
From: jkeen@concentric.net (James E Keenan)
Subject: Re: Devel::Coverage
Message-Id: <b955da04.0307011104.6f41f377@posting.google.com>
himal5@hotmail.com (Himal) wrote in message news:<365e1935.0307010656.8378c1d@posting.google.com>...
> Hi all,
> I am new to using Perl and am trying to do some coverage analysis.
> When I do
> perl -d:Coverage temp.pl
> I get Cant locate Devel/Coverage.pm. What should I do for this.
>
Devel::Coverage is not included in the standard Perl distribution.
A quick test I use in this sitation: At the command prompt, type
"perldoc Devel::Coverage". If the response is no documentation
available, then, at the very least, you don't have any documentation
for that module and, in 99+% of the cases, the module is not loaded.
If you have the proper permissions, you can load it yourself from
CPAN:
http://search.cpan.org/author/RJRAY/Devel-Coverage-0.2/Coverage.pm
Note: the documentation on CPAN describes this module as "still very
early alpha-quality" -- which of course means that it would not be
included in any standard Perl distribution and why you do not have it
loaded.
HTH
Jim Keenan
------------------------------
Date: 1 Jul 2003 11:51:09 -0700
From: stam_doar@hotmail.com (Dov Levenglick)
Subject: Re: funny printf behaviour in need of explanation
Message-Id: <8ebf3385.0307011051.536898c5@posting.google.com>
>
> Using perl 5.005_03, as provided with Solaris 8, I too get these
> results. However, running against 5.8.0 (compiled using gcc 3.3 from
> sunfreeware.com) I get this regardless of whether the debugging printf
> is used or not:
>
>
> I know this doesn't answer your actual question, but maybe installing
> 5.8.0 (alongside 5.005_03) may be an option for you.
>
as a matter of fact this can point to a problem with PERL in versions
previous to 5.8.0, since the behaviour that you observed is the
expected one.
if anyone else, however, has insights to the cause of this peculiar
behaviour, i would love to hear about it.
------------------------------
Date: 1 Jul 2003 12:15:59 -0700
From: jkeen@concentric.net (James E Keenan)
Subject: Re: funny printf behaviour in need of explanation
Message-Id: <b955da04.0307011115.749f99ad@posting.google.com>
stam_doar@hotmail.com (Dov Levenglick) wrote in message news:<8ebf3385.0307010520.5ff0e1e4@posting.google.com>...
> Hi,
> i am attaching a snippet of code and two results that it produces. the
> first result is when the printf line is not commented out while the
> second result is when the line is commented. the code was run on a
> UNIX station running SunOS 5.8
>
> result #1:
> big_endian: 123456 78
> big_endian: 1234 7856
> big_endian: 12 785634
> big_endian: 0 78563412
> Result: 12345678 78563412
>
> result #2:
> Result: 12345678 ffffffff
>
> i am aware that $a reaches the value of 0 only after many divisions by
> 256 (since divisions are in floating point context) and therefore
> $result reaches Infinity (hence ffffffff), my question is how and why
> does the printf change the context of $a from floating point to
> integer.
>
> btw, i am aware that "use integer" or $a=int($a/256)" would avoid the
> problem as well, therefore please dont offer them as an answer to my
> question.
>
> my $a = 0x12345678;
> $b = &big_endian($a);
> printf ("Result: %x %x\n",$a,$b);
>
You should not be using $a and $b as assignable variables. In Perl 5
they are reserved for use by the sort function.
Jim Keenan
------------------------------
Date: Tue, 1 Jul 2003 21:05:42 +0000 (UTC)
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: How do you sort a 2D array with column headers?
Message-Id: <bdst36$coq$1@plover.com>
In article <20030630132321.238$lk@newsreader.com>, <ctcgag@hotmail.com> wrote:
>I wish the ".." operator, when occuring in a slice, were sufficiently
>magical to allow @{$m}[1..-1] to replace the above.
Someone, I think Simon Cozens, submitted a patch to allow
@a[1..]
to do this. But it wasn't accepted; I forget why not.
>> 2. The upper bound of a C-style 'for' loop, as
>>
>> for ($i=0; $i <= $#array; $i++) {
>> do something with $array[$i];
>> }
>
>I use this very frequently when I have parallel arrays. Of course,
>that might not exactly fit in your criteria for inclusion in this
>category.
It does. Ignoring the fact that parallel arrays are usually a sign of
misdesign in the program, you can write the code above more simply
and efficiently as:
for $i (0 .. $#array) {
do something with $array[$i];
}
>I also use this when I want to change the length of @array
>during the loop.
In such cases it makes perfect sense.
------------------------------
Date: Tue, 01 Jul 2003 23:29:10 +0200
From: Stefan Glimne <Snygg@gmx.net>
Subject: Re: Not following "if"
Message-Id: <3f01fd88$0$21974$9b4e6d93@newsread4.arcor-online.net>
Well, I have now tested "exists", and it does not help. And there is
nothing wrong with the code (exept that I forgot to show where I get the
value $filmid. $filmid is stored like this:
.....
if ($line eq "##########") {
$line = <INDATA>;
chop $line;
($key,$filmid) = split(" = ", $line);
} else {
($key,$value) = split(" = ", $line);
$key2 = $filmid . '_' . $key;
.....
Let me explain the problem in another way:
I need to store data in a database, but first I have to check if the
data already exist. So far so good. When I have done the check, and if
it doesn't exist (and I KNOW that it does not exist!!!!) the program
writes the data in the database. so far so good. now suddenly the
program decides, after storing the BEFORE-NON-EXISTING-DATA, that the
data exists in the database, even if I store the data after the check.
And now the fun part of it comes:
When I do it local on my computer (which is the server) it does NOT work
most of the times. But when a friend does it over internet it ALLWAYS
work for him. 2 me it sounds as if the program goes to fast when I do it
local. Is there another way to open the database file and write things
in it? Would it work better if I did it with subroutine?
Fun part #2:
If I take away the storing of the new data in the database, the program
works fine. Taht means that there is something wrong with using the
if-statement and having an open-write within the if-statement. Is there
a logical explanation to this???
Thanx in advance, and thank U for your nice help so far
/Stefan
Eric Amick wrote:
> On Sat, 28 Jun 2003 16:25:02 +0200, Stefan Glimne <Snygg@gmx.net> wrote:
>
>
>>Hello 2 u all,
>>
>>I have a really interesting problem that I get when I program my
>>html-pages with perl.
>>The problem is that the perl-program doesn't follow the if-statement.
>>
>>1. I get info from a standard text-file database
>>2. I store an hash like , $imdblink{$imdbid} = $value , from the
>> database
>>3. Then I have a new number $imdbid_new that I want to check if it's in
>> the $imdblink-hash like ,
>> if ($imdblink{$imdbid_new}) {
>> "if it exist then this should happen"
>> } else {
>> "if it doesn't exist then this should happen"
>> }
>
> [snip]
>
>>Here is the example that does not work:
>>
>>##############################################
>>#
>># Load Films in FilmDataBase
>>#
>>$file = "db\\filmdatabase_comp.txt";
>>open (INDATA, "$home_dir$file") or die "Can't open $home_dir$file: $!\n";
>>while ($line = <INDATA>) {
>> chop $line;
>
>
> chomp is a better choice.
>
>
>> ($key,$value) = split(" = ", $line);
>> $key2 = $filmid . '_' . $key;
>> $filmdatabase{$key2} = $value;
>> if ($key eq "imdblink") {
>> ($key,$imdbid) = split('\?', $line);
>> $imdblink{$imdbid} = $filmid;
>
>
> Where does $filmid get a value? If it's uninitialized or 0, that
> explains your whole problem--the later if test will always be false,
> even if the hash entry exists. Using exists as we've already suggested
> is definitely better than testing the value, but it appears you're
> storing the wrong value anyway.
>
>
>> }
>>}
>>close INDATA;
>
>
------------------------------
Date: Tue, 1 Jul 2003 13:57:54 -0700
From: "Brian Helterline" <brian_helterline@hp.com>
Subject: Re: OLE Variant
Message-Id: <3f01faba$1@usenet01.boi.hp.com>
Hi Ken,
"Ken Brown" <ken.brown@dialwrap.co.uk> wrote in message
news:bdshcf$t60$1@news6.svr.pol.co.uk...
> Trying to get excel formatted as dd/mm/yyyy into perl variable
>
> seems to be ruturning ole::variant but i cant get any sense out of return
> date
>
> $$item = Variant(VT_DATE, "$sheet->Cells(6,$CurCol)->{Value}");
Syntax Error - there should not be quotes around $sheet.....
> $$item = $$item->Date("dd/MM/yyyy");
>
> it keeps returing 30/12/1899 - not overly helpful
>
> any ideas?
>
A funny coincidence, if you run this:
my $item = Variant(VT_DATE, 0 );
print $item->Date( "dd/MM/yyyy" );
what value to you get? (hint: 30/12/1899)
It seems you don't have a value in Cells(6,$CurCol)
I think you may be over working the problem. The value returned from the
cell
will already be a string that looks like a date. (At least it does on my
system)
One thing to note: if Excel has the date formatted as "dd/mm/yyyy", when I
get
the value of the cell, it looks like "mm/dd/yyyy" which is my default.
E.g. On my system, if the excel cell has the format dd/mm/yyyy and contains
Jan 15, 2003, Excel displays it as 15/01/2003 but if you execute:
print $sheet->Cells(6,$CurCol)->{Value}
I get '1/15/2003'
-brian
------------------------------
Date: Tue, 1 Jul 2003 22:37:26 -0700
From: "Ken Brown" <ken.brown@dialwrap.co.uk>
Subject: Re: OLE Variant
Message-Id: <bdsupe$h4b$1@newsg4.svr.pol.co.uk>
Checked it out and you were right it does return a date once I sorted things
out
But if the field has a custom date format then it returns a variant
And god only knows how excel is outputting as nothing will format it
correctly
However - simple approach - I just reformat cell before I read it so I know
what I'm reading.
Thanks for the sanity check - simple is best - I just got a bit carried away
for a while
Ken
"Brian Helterline" <brian_helterline@hp.com> wrote in message
news:3f01faba$1@usenet01.boi.hp.com...
> Hi Ken,
>
> "Ken Brown" <ken.brown@dialwrap.co.uk> wrote in message
> news:bdshcf$t60$1@news6.svr.pol.co.uk...
> > Trying to get excel formatted as dd/mm/yyyy into perl variable
> >
> > seems to be ruturning ole::variant but i cant get any sense out of
return
> > date
> >
> > $$item = Variant(VT_DATE, "$sheet->Cells(6,$CurCol)->{Value}");
>
> Syntax Error - there should not be quotes around $sheet.....
>
> > $$item = $$item->Date("dd/MM/yyyy");
> >
> > it keeps returing 30/12/1899 - not overly helpful
> >
> > any ideas?
> >
>
> A funny coincidence, if you run this:
>
> my $item = Variant(VT_DATE, 0 );
> print $item->Date( "dd/MM/yyyy" );
>
> what value to you get? (hint: 30/12/1899)
>
> It seems you don't have a value in Cells(6,$CurCol)
>
> I think you may be over working the problem. The value returned from the
> cell
> will already be a string that looks like a date. (At least it does on my
> system)
>
> One thing to note: if Excel has the date formatted as "dd/mm/yyyy", when I
> get
> the value of the cell, it looks like "mm/dd/yyyy" which is my default.
> E.g. On my system, if the excel cell has the format dd/mm/yyyy and
contains
> Jan 15, 2003, Excel displays it as 15/01/2003 but if you execute:
>
> print $sheet->Cells(6,$CurCol)->{Value}
>
> I get '1/15/2003'
>
> -brian
>
>
------------------------------
Date: Tue, 1 Jul 2003 14:06:03 -0700
From: "Brian Helterline" <brian_helterline@hp.com>
Subject: Re: Problem with tk and displaying on the screen
Message-Id: <3f01fca4$1@usenet01.boi.hp.com>
"derek / nul" <abuse@sgrail.org> wrote in message
news:q2g3gvka9ougjqmk7ejjhvga0n8b7hhbmj@4ax.com...
> good call and thank you.
>
> The use tk; was wrong
This is a nasty little feature of Windows (or any case-insensitive OS)
When it looks for the file to 'use', it succeeds since the OS is doing
the searching in a case-insensitive manner. When perl looks for the
functions
that were imported, it does it itself in a case-sensitive manner and fails!
Anyone know of a way to fix this?
-brian
{snip rest of thread}
------------------------------
Date: 1 Jul 2003 11:19:43 -0700
From: danielf@erols.com (Daniel Friedman)
Subject: Re: Q: detect non-existent hash entries w/function?
Message-Id: <68cdce23.0307011019.54eeb3db@posting.google.com>
There was apparently some ambiguity in my earlier post, so let me try
to clarify.
1) Purpose/motivation: I'm writing a script which will have lots of
places in which I want to carefully check for the existence of a hash
entry, and also avoid autovivification (I admit that the
non-autovification piece was not in my original post, I omitted it to
try to focus my question), before getting the value from the
hash--here's what I mean:
$errhint = __FILE__; # this is just an example
unless (exists $myhash{$foo}) {
die "$errhint: myhash{$foo} does not exist";
}
unless (exists $myhash{$foo}{$bar}) {
die "$errhint: myhash{$foo}{$bar} does not exist";
}
unless (exists $myhash{$foo}{$bar}{$baz}) {
die "$errhint: myhash{$foo}{$bar}{$baz} does not exist";
}
# apparently it exists, so let's get the value:
$thevalue = $myhash{$foo}{$bar}{$baz}
---> I want to make a function which does this. The function's
arguments
should be the "specification" of a hash whose existence is to be
checked, and some string for helping find the error ($errhint, above).
(The function would preferably be able to handle a non-fixed number
of hash levels.)
2) When I wrote in my earlier post "maybe a scalar, maybe not", I
meant that I wasn't fussy if the function receives the specification
as a single scalar argument (e.g. "myhash{$foo}{$bar}{$baz}") or in
some other form ("myhash", "$foo", "$bar", "$baz"). I think it can't
be a reference since I don't imagine it's possible to make a reference
to something which doesn't necessarily exist.
I hope that clarifies my question. Again, thanks very much,
--daniel
------------------------------
Date: Tue, 01 Jul 2003 18:40:14 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Q: detect non-existent hash entries w/function?
Message-Id: <x7el1a6qgh.fsf@mail.sysarch.com>
>>>>> "DF" == Daniel Friedman <danielf@erols.com> writes:
DF> There was apparently some ambiguity in my earlier post, so let me try
DF> to clarify.
DF> 1) Purpose/motivation: I'm writing a script which will have lots of
DF> places in which I want to carefully check for the existence of a hash
DF> entry, and also avoid autovivification (I admit that the
DF> non-autovification piece was not in my original post, I omitted it to
DF> try to focus my question), before getting the value from the
DF> hash--here's what I mean:
first level hash lookups never autovivify. only multilevel lookups do
that. basically autoviv happens when you dereference through undef. see
my paper on this at:
http://tlc.perlarchive.com/articles/perl/ug0002.shtml
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: Tue, 1 Jul 2003 15:52:11 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Q: detect non-existent hash entries w/function?
Message-Id: <slrnbg3t3r.2tf.tadmc@magna.augustmail.com>
Daniel Friedman <danielf@erols.com> wrote:
> There was apparently some ambiguity in my earlier post, so let me try
> to clarify.
>
> 1) Purpose/motivation: I'm writing a script which will have lots of
> places in which I want to carefully check for the existence of a hash
> entry, and also avoid autovivification (I admit that the
> non-autovification piece was not in my original post, I omitted it to
> try to focus my question),
Errr, but that *is* your question:
detect non-existent hash entries w/o autoviv'ing
The answer to your OP questions was:
exists $myhash{$foo}{$bar}{$baz}
which doesn't avoid the autoviving for you.
> ---> I want to make a function which does this. The function's
> arguments
> should be the "specification" of a hash whose existence is to be
> checked, and some string for helping find the error ($errhint, above).
> (The function would preferably be able to handle a non-fixed number
> of hash levels.)
>
> 2) When I wrote in my earlier post "maybe a scalar, maybe not", I
> meant that I wasn't fussy if the function receives the specification
> as a single scalar argument (e.g. "myhash{$foo}{$bar}{$baz}") or in
> some other form ("myhash", "$foo", "$bar", "$baz").
> I hope that clarifies my question.
It doesn't clarify your question, it states your question. :-)
Here is a function to get you started, it wants a hash-ref as
the first arg, followed by a list of keys, eg:
do_something() if exists_no_viv(\%myhash, $foo, $bar, $baz)
---------------------------
sub exists_no_viv { # "walk" down a chain of hashrefs
my $h = shift; # a ref to hash, start at the "top"
while ( my $k = shift ) {
return 0 unless exists $h->{$k}; # fail w/o autoviv
if ( ref $h->{$k} eq 'HASH' ) # go one level "deeper"
{ $h = $h->{$k} }
}
return 1;
}
---------------------------
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Tue, 01 Jul 2003 19:17:27 +0100
From: Tux <colin@murorum.demon.co.uk>
Subject: Splitting text from a database table
Message-Id: <bdsj7n$jqp$1$8302bc10@news.demon.co.uk>
I have some text which I have extracted from a database table, this contains
embedded CR/LF pairs, i.e. \r\n.
I would like to split the text into separate strings, breaking at the CR/LF
pair, but I just cannot work out how to do it. Would anybody take pity on a
newbie and give me a clue.
--
Colin Walls
Email: Colin.Walls@murorum.demon.co.uk
Web: http://www.murorum.demon.co.uk
Phone: 01625 535123
Mobile: 07765 175829
------------------------------
Date: Tue, 01 Jul 2003 18:34:20 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Splitting text from a database table
Message-Id: <vg3l1c3rlteh21@corp.supernews.com>
In article <bdsj7n$jqp$1$8302bc10@news.demon.co.uk>,
Tux <colin@murorum.demon.co.uk> wrote:
: I have some text which I have extracted from a database table, this
: contains embedded CR/LF pairs, i.e. \r\n.
:
: I would like to split the text into separate strings, breaking at the
: CR/LF pair, but I just cannot work out how to do it. Would anybody
: take pity on a newbie and give me a clue.
What have you tried? Why not something such as the following?
@lines = split /\r\n/, $chunk;
Hope this helps,
Greg
--
When Galileo turned his telescope toward the heavens, and allowed Kepler to
look as well, they found no enchantment or authorization in the stars, only
geometric patterns and equations. God, it seemed, was less of a moral
philosopher than a master mathematician. -- Neil Postman
------------------------------
Date: Tue, 1 Jul 2003 20:53:10 +0200
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: Splitting text from a database table
Message-Id: <Pine.LNX.4.53.0307012044060.16131@lxplus077.cern.ch>
On Tue, Jul 1, Tux inscribed on the eternal scroll:
> I have some text which I have extracted from a database table, this contains
> embedded CR/LF pairs, i.e. \r\n.
If you want to write portably, you will avoid coding \r\n when you
mean CRLF. Refer to perldoc perlport (the bit about sockets
programming is what I'm alluding to).
http://www.perldoc.com/perl5.8.0/pod/perlport.html#Newlines
That's assuming that your data would still contain CRLF even when the
script was running on a Mac. If, on the other hand, you expected the
data to still contain \r\n (i.e LFCR) on a Mac, then go right ahead...
> I would like to split the text into separate strings, breaking at the CR/LF
> pair,
Using split with /\015\012/ will do the trick. As the perlport
document indicates, if you like it better you can import $CRLF from
the Socket module and use that for its mnemonic benefit - the actual
data would be the same.
cheers
--
"The operator has a long-standing problem with short formations" -
news report about a train service - translation: the operator
provides a short train, and the travellers get a long stand.
------------------------------
Date: Tue, 01 Jul 2003 20:23:16 +0200
From: Matija Papec <mpapec@yahoo.com>
Subject: Re: Using undef as an array subscript
Message-Id: <nvj3gvgi8qj8aq376jrir183fgatjtkrli@4ax.com>
X-Ftn-To: JS Bangs
JS Bangs <jaspax@u.washington.edu> wrote:
>> : @c[grep { defined($_) } @a] = @b;
>>
>> That doesn't throw away the corresponding elements of @b.
>
>???? I don't understand what you mean by "throw away the corresponding
>elements of @b". Based on the original code we saw, the OP doesn't seem to
>want to alter @b at all, but only to make @c into a subset of @b. But
>perhaps I've missed something. Can the OP herself clarify?
@a and @b, each have same number of elements, so when removing from @a, one
should remove corresponding element from @b, so @c[@a] = @b could work as
expected.
--
Matija
------------------------------
Date: 01 Jul 2003 12:28:21 -0600
From: Eric Schwartz <emschwar@pobox.com>
Subject: Re: Using undef as an array subscript
Message-Id: <eto3chqgkze.fsf@wormtongue.emschwar>
JS Bangs <jaspax@u.washington.edu> writes:
> ???? I don't understand what you mean by "throw away the corresponding
> elements of @b".
The OP's code always shifted off an entry from @b (well, from a copy
of it named @bb) for every element of @a, even if that element was
undefined. Your code assumes that
scalar(@b) == scalar(grep { defined($_) } @a )
which is not necessarily true.
> Based on the original code we saw, the OP doesn't seem to
> want to alter @b at all, but only to make @c into a subset of @b. But
> perhaps I've missed something. Can the OP herself clarify?
Based on the original code, the idea is that @c contains all the
elements of $b for which the corresponding element in $a is defined.
I think Greg's point was that you forgot that @b may contain elements
that correspond to undefined elements in @a, and thus shouldn't be
included.
To the OP: You can adapt JS' code to not explicitly require a loop,
but a loop is going to happen anyway, whether you make it explicit or
not. I think you'll find that using the loop code is going to be more
readable and maintainable than more arcane trickery.
-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
------------------------------
Date: Tue, 01 Jul 2003 18:31:17 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Using undef as an array subscript
Message-Id: <vg3krl3bi079e0@corp.supernews.com>
In article <Pine.A41.4.55.0307011037430.83754@dante14.u.washington.edu>,
JS Bangs <jaspax@u.washington.edu> wrote:
: Greg Bacon sikyal:
:
: > In article <Pine.A41.4.55.0307010922210.83754@dante14.u.washington.edu>,
: > JS Bangs <jaspax@u.washington.edu> wrote:
: >
: > : @c[grep { defined($_) } @a] = @b;
: >
: > That doesn't throw away the corresponding elements of @b.
:
: ???? I don't understand what you mean by "throw away the corresponding
: elements of @b". Based on the original code we saw, the OP doesn't seem to
: want to alter @b at all, but only to make @c into a subset of @b. But
: perhaps I've missed something. Can the OP herself clarify?
Here's the code the OP sought to avoid:
@bb = @b;
foreach $a (@a) {
my $b = shift @bb;
next unless defined $a;
$c[$a] = $b;
}
Say we have the following setup:
@a = (2, undef, undef, 0, 1);
@b = (7, 8, 42, 55, 33);
The OP's code effectively produces
@c = (55, 33, 7);
but yours
@c = (8, 42, 7);
That's because your version isn't throwing away the elements of @b
that correspond to undefined elements of @a.
Greg
--
History teaches that grave threats to liberty often come in times of
urgency, when constitutional rights seem too extravagant to endure.
-- Justice Thurgood Marshall
------------------------------
Date: Tue, 01 Jul 2003 18:59:59 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Using undef as an array subscript
Message-Id: <vg3mhf2gu9ri63@corp.supernews.com>
In article <75e50dfd.0307010730.63f6253@posting.google.com>,
Yehuda Berlinger <jon@actcom.co.il> wrote:
: [...]
:
: I was hoping to avoid doing a loop such as:
:
: @bb = @b;
: foreach $a (@a) {
: my $b = shift @bb;
: next unless defined $a;
: $c[$a] = $b;
: }
:
: Any thoughts?
You could do it this way:
#! /usr/local/bin/perl
use warnings;
use Data::Dumper;
sub zip {
use strict;
my @a = @{ $_[0] || [] };
my @b = @{ $_[1] || [] };
my @result;
push @result => [shift @a, shift @b] while @a || @b;
@result;
}
@a = (0, undef);
@b = (1, 2);
$c[ $_->[0] ] = $_->[1] for grep defined($_->[0]), zip \@a, \@b;
print Dumper \@c;
Don't do it that way, though; it has little value outside a puzzle or
challenge. If we had a better idea of what you're trying to model,
we might be able to offer better suggestions.
Perl 6 will have parallel traversal, so you'll someday be able to
for @a; @b -> $a; $b {
@c[$a] = $b if defined $a;
}
Greg
--
When a true genius appears in this world, you may know him by this sign,
that the dunces are all in confederacy against him.
-- Jonathan Swift
------------------------------
Date: Tue, 01 Jul 2003 19:04:19 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Using undef as an array subscript
Message-Id: <vg3mpjf95sf7b9@corp.supernews.com>
In article <vg3mhf2gu9ri63@corp.supernews.com>,
Greg Bacon <gbacon@hiwaay.net> wrote:
: Perl 6 will have parallel traversal, so you'll someday be able to
:
: for @a; @b -> $a; $b {
: @c[$a] = $b if defined $a;
: }
Sorry, no reference. The foreshadowing comes from Apocalypse 4,
RFC 173. The URL is hideous, so visit
http://makeashorterlink.com/?X6E411E15
Greg
--
. . . but the twin deficits [trade and Federal] aren't just theoretical
economic issues anymore. They're starting to end careers in Washington.
Maybe deficits aren't all bad after all . . .
-- Hans Sennholz, "Deficits Do Matter"
------------------------------
Date: Tue, 1 Jul 2003 12:19:38 -0700
From: JS Bangs <jaspax@u.washington.edu>
Subject: Re: Using undef as an array subscript
Message-Id: <Pine.A41.4.55.0307011213200.83754@dante14.u.washington.edu>
Greg Bacon sikyal:
> : > : @c[grep { defined($_) } @a] = @b;
> : >
> : > That doesn't throw away the corresponding elements of @b.
> :
> : ???? I don't understand what you mean by "throw away the corresponding
> : elements of @b". Based on the original code we saw, the OP doesn't seem to
> : want to alter @b at all, but only to make @c into a subset of @b. But
> : perhaps I've missed something. Can the OP herself clarify?
>
> Here's the code the OP sought to avoid:
> [snip illuminating code examples]
>
> The OP's code effectively produces
>
> @c = (55, 33, 7);
>
> but yours
>
> @c = (8, 42, 7);
>
> That's because your version isn't throwing away the elements of @b
> that correspond to undefined elements of @a.
Ah. I had misunderstood what the OP was hoping to do. In this case, the
foreach does seem to be like the most readable solution.
Jesse S. Bangs jaspax@u.washington.edu
http://students.washington.edu/jaspax/
http://students.washington.edu/jaspax/blog
------------------------------
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 5164
***************************************