[31717] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2980 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jun 8 21:09:25 2010

Date: Tue, 8 Jun 2010 18: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           Tue, 8 Jun 2010     Volume: 11 Number: 2980

Today's topics:
    Re: convert integer to string <cartercc@gmail.com>
    Re: convert integer to string <cartercc@gmail.com>
    Re: convert integer to string <uri@StemSystems.com>
    Re: convert integer to string <uri@StemSystems.com>
    Re: convert integer to string <cartercc@gmail.com>
    Re: convert integer to string <tadmc@seesig.invalid>
    Re: convert integer to string <m@rtij.nl.invlalid>
    Re: convert integer to string <ben@morrow.me.uk>
    Re: convert integer to string <tadmc@seesig.invalid>
    Re: convert integer to string <cartercc@gmail.com>
    Re: convert integer to string <jurgenex@hotmail.com>
    Re: convert integer to string <jurgenex@hotmail.com>
    Re: How to read a given number of lines? <willem@turtle.stack.nl>
    Re: How to read a given number of lines? <marc.girod@gmail.com>
    Re: How to read a given number of lines? <ben@morrow.me.uk>
    Re: stuff <uri@StemSystems.com>
    Re: stuff <spamtrap@shermpendley.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 8 Jun 2010 13:16:20 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: convert integer to string
Message-Id: <4a29345e-1bd7-4b3b-aea3-30ed90ca4f89@t10g2000yqg.googlegroups.com>

On Jun 8, 4:05=A0pm, Willem <wil...@turtle.stack.nl> wrote:
> ) This is NOT the problem here. What this does is make the hash element
> ) $fac{$id}{courses} contain a scalar value like this:
> ) '23456 34567 45678' This works perfectly and does exactly what I want
> ) it to.
>
> No, it doesn't.

I beg to differ, but it does. I've been running this particular piece
of code for about three years, and it has exactly the behavior I
described. This is a line from my debugging file with only the
personal information replaced with XXXXX.

1073251 =3D> HASH(0x1e4ea6c)
     xlist =3D>
     middle =3D> A
     first =3D> XXXXX
     contract =3D> XXXXX
     csz =3D> XXXXX
     addy2 =3D>
     courses =3D> 235519 235524 237125
     last =3D> XXXXX
     id2 =3D> 1073251
     addy1 =3D> XXXXX Drive
     mail =3D> XXXXX@XXXXX
     trs =3D> Current Member


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

Date: Tue, 8 Jun 2010 13:27:36 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: convert integer to string
Message-Id: <0a177055-e7a9-4f98-9447-745a68a9450d@a30g2000yqn.googlegroups.com>

On Jun 8, 2:10=A0pm, ccc31807 <carte...@gmail.com> wrote:
> This is embarrassing!

Okay, now I really am embarrassed. It's not a Perl problem at all --
it's a Microsoft problem.

The script that I reference is the third one out of four, the whole
process takes about six input files and outputs several thousand PDF
files. I get the data from various people and a couple of databases.

I get some of the data in CSV format. One of my sources switched from
an Access database to an Excel file. Turns out that Excel strips out
the leading zeros if it thinks that the datum is an integer.

I really, really should have learned this lesson by now -- check the
code, check the data. Yes, I mostly validate the data as it comes in,
checking the format and so on, and the particular numeric datum I used
as a key validated as numeric. It never occurred to me to look at the
data file until after I spent several hours checking and rechecking my
code and posting on c.l.p.m.

'Garbage in, garbage out' isn't always the result of bad code, it can
be the result of bad data. Thanks to all, and please accept my apology
for the excitement.

CC.


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

Date: Tue, 08 Jun 2010 16:38:53 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: convert integer to string
Message-Id: <87r5khxns2.fsf@quad.sysarch.com>

>>>>> "c" == ccc31807  <cartercc@gmail.com> writes:

  c> On Jun 8, 4:05 pm, Willem <wil...@turtle.stack.nl> wrote:
  >> ) This is NOT the problem here. What this does is make the hash element
  >> ) $fac{$id}{courses} contain a scalar value like this:
  >> ) '23456 34567 45678' This works perfectly and does exactly what I want
  >> ) it to.
  >> 
  >> No, it doesn't.

  c> I beg to differ, but it does. I've been running this particular piece
  c> of code for about three years, and it has exactly the behavior I
  c> described. This is a line from my debugging file with only the
  c> personal information replaced with XXXXX.

then that is not the code that you are using. it will put the list of
courses into the hash as key/value pairs. the only way you get what you
claim is with "@courses". did you lose the quotes in pasting? if you
claim that, show exact runnable code that does this. you can whip up an
dummy example in 2 minutes. here is one:

perl -MData::Dumper -e '@x = ( 1 .. 4 ) ; %y = (x => @x); print Dumper \%y'
$VAR1 = {
          '4' => undef,
          'x' => 1,
          '2' => 3
        };

as seen, it doesn't do what you claim it does. possibly the dump trick
you are using is misleading you. use data::dumper to see what is really
there. here is what you seem to want:

perl -MData::Dumper -e '@x = ( 1 .. 4 ) ; %y = (x => "@x"); print Dumper \%y'
$VAR1 = {
          'x' => '1 2 3 4'
        };


or alternatively with a ref:

perl -MData::Dumper -e '@x = ( 1 .. 4 ) ; %y = (x => \@x); print Dumper \%y'
$VAR1 = {
          'x' => [
                   1,
                   2,
                   3,
                   4
                 ]
        };


uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 08 Jun 2010 16:40:04 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: convert integer to string
Message-Id: <87mxv5xnq3.fsf@quad.sysarch.com>

>>>>> "c" == ccc31807  <cartercc@gmail.com> writes:

  c> 'Garbage in, garbage out' isn't always the result of bad code, it
  c> can be the result of bad data. Thanks to all, and please accept my
  c> apology for the excitement.

you still have a bug if you claim x => @y will do what you want. see my
other post on this.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 8 Jun 2010 14:12:53 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: convert integer to string
Message-Id: <154e201a-3b5d-4a42-99ca-c1a535af9032@t10g2000yqg.googlegroups.com>

On Jun 8, 4:40=A0pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> you still have a bug if you claim x =3D> @y will do what you want. see my
> other post on this.

-----------SCRIPT---------------
#! perl
# array.plx
use strict;
use warnings;
my %presidents;
while (<DATA>)
{
	chomp;
	my ($order, $first, $last, @years) =3D split /\|/;
	$presidents{$order} =3D {
		first =3D> $first,
		last =3D> $last,
		years =3D> @years,
	};
}

foreach my $k (sort keys %presidents)
{
	print "$k =3D> $presidents{$k}\n";
	foreach my $k2 (sort keys %{$presidents{$k}})
	{
		print "   $k2 =3D> $presidents{$k}{$k2}\n";
	}
}
exit(0);

__DATA__
1|George|Washington|1788 1792
2|John|Adams|1796
3|Thomas|Jefferson|1800 1804
4|James|Madison|1808 1812
32|Franklin|Roosevelt|1932 1936 1940 1944

----------OUTPUT----------------
D:\PerlLearn>perl array.plx
01 =3D> HASH(0x248e5c)
   first =3D> George
   last =3D> Washington
   years =3D> 1788 1792
02 =3D> HASH(0x182a344)
   first =3D> John
   last =3D> Adams
   years =3D> 1796
03 =3D> HASH(0x182a3b4)
   first =3D> Thomas
   last =3D> Jefferson
   years =3D> 1800 1804
04 =3D> HASH(0x182a8a4)
   first =3D> James
   last =3D> Madison
   years =3D> 1808 1812
32 =3D> HASH(0x183ce44)
   first =3D> Franklin
   last =3D> Roosevelt
   years =3D> 1932 1936 1940 1944


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

Date: Tue, 08 Jun 2010 16:47:22 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: convert integer to string
Message-Id: <slrni0te7s.lq6.tadmc@tadbox.sbcglobal.net>

ccc31807 <cartercc@gmail.com> wrote:
> On Jun 8, 4:40 pm, "Uri Guttman" <u...@StemSystems.com> wrote:
>> you still have a bug if you claim x => @y will do what you want. see my
>> other post on this.
>
> -----------SCRIPT---------------
> #! perl
> # array.plx
> use strict;
> use warnings;
> my %presidents;
> while (<DATA>)
> {
> 	chomp;
> 	my ($order, $first, $last, @years) = split /\|/;
> 	$presidents{$order} = {
> 		first => $first,
> 		last => $last,
> 		years => @years,
> 	};
> }

[snip]

> __DATA__
> 1|George|Washington|1788 1792
> 2|John|Adams|1796
> 3|Thomas|Jefferson|1800 1804
> 4|James|Madison|1808 1812
> 32|Franklin|Roosevelt|1932 1936 1940 1944


@years always contains exactly one element, it is a non-arrayish array.

$years would work as well, and would avoid looking like it wouldn't
work...


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Tue, 8 Jun 2010 23:49:30 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: convert integer to string
Message-Id: <agq2e7-p39.ln1@news.rtij.nl>

On Tue, 08 Jun 2010 14:12:53 -0700, ccc31807 wrote:

> On Jun 8, 4:40 pm, "Uri Guttman" <u...@StemSystems.com> wrote:
>> you still have a bug if you claim x => @y will do what you want. see my
>> other post on this.
> 
> -----------SCRIPT---------------
(snip)
> 	my ($order, $first, $last, @years) = split /\|/; $presidents
{$order} =
> 	{
> 		first => $first,
> 		last => $last,
> 		years => @years,
> 	};
(snip)
> 
> __DATA__
> 1|George|Washington|1788 1792
(snip)

This only "works" because @years has only one element, the string "1788 
1792". It is still wrong, wrong, wrong. Fix your code before someone tags 
on another element on the end and everything breaks.

HTH,
M4


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

Date: Tue, 8 Jun 2010 22:52:52 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: convert integer to string
Message-Id: <kmq2e7-km52.ln1@osiris.mauzo.dyndns.org>


Quoth "Uri Guttman" <uri@StemSystems.com>:
> 
> in that limited code i don't see where the keys would be interpreted as
> literal numbers. there must be something else going on which is doing
> that. perl won't lose leading zeroes in strings without doing some
> number conversions. are you sure you never look at those kays as
> numbers? like use == to check them or similar? since they seem to be
> fixed size you can always use the string comparison ops safely.

Comparing with == does no harm. When a scalar has been converted from
string to number, perl remembers that the string value is canonical and
will not lose it. (This is similar to the float/int tricks it does to
keep all the precision possible.)

What *does* do harm is performing arithmetic, or other numeric
operations, and using the result rather than the original. That is:

    my $x = "01";
    $x == 1;
    # $x is still "01"
    $x + 1;
    # $x is still "01"
    $x += 1;
    # $x is now 2, and will stringify as "2"

(this also applies to the redundant case of

    $x += 0;

, which is one of the ways of forcing a scalar to be numeric.)

Ben



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

Date: Tue, 08 Jun 2010 19:02:29 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: convert integer to string
Message-Id: <slrni0tm58.m0u.tadmc@tadbox.sbcglobal.net>

ccc31807 <cartercc@gmail.com> wrote:

>    my ($last, $first, $middle, $id1, $filename, $crs_id, $site, $loc,
> $glcode, $level, $count, $status, $section, $title, $hours, $xlist,
> $total, $travel, $contract) = parse_line(',', 0, $_);
> 	next if $contract =~ /N/;
> 	$sec{$crs_id} = {
> 	crs_id => $crs_id,
> 	filename => $filename,
> 	id1 => $id1,
> 	site => $site,
> 	loc => $loc,
> 	glcode => $glcode,
> 	level => $level,
> 	count => $count,
> 	status => $status,
> 	section => $section,
> 	title => $title,
> 	hours => $hours,
> 	xlist => $xlist,
> 	total => $total,
> 	travel => $travel,
> 	contract => $contract,
> 	};


Hash slice to the rescue! (untested)

    my @fields = qw(
        crs_id
        filename
        id1
        site
        loc
        glcode
        level
        count
        status
        section
        title
        hours
        xlist
        total
        travel
        contract
    );
    @{ $sec{$crs_id} }{@fields} = parse_line(',', 0, $_);
    next if $sec{$crs_id}{contract} =~ /N/;


Look Ma! Each name is typed only once, and there aren't
a bazillion temporary variables!


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Tue, 8 Jun 2010 18:00:00 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: convert integer to string
Message-Id: <84b4b9db-1bc6-4d45-9832-6c19bb29da0b@j8g2000yqd.googlegroups.com>

On Jun 8, 5:47=A0pm, Tad McClellan <ta...@seesig.invalid> wrote:
> @years always contains exactly one element, it is a non-arrayish array.
>
> $years would work as well, and would avoid looking like it wouldn't
> work...
>

You are right.

As an explanation, not an excuse, the &rest parameter in Lisp takes
the rest of the arguments and flattens all lists. I've found this very
useful in manipulating Lisp data, and guess I was half asleep at the
wheel, channeling Lisp while writing Perl.

What I saw was 'courses' as an array, and in fact use @courses later
on in the script to iterate through the elements, and was thinking
'list' when I should have seen 'scalar.'

My bad, and now I'm triple embarrassed. Uri and the others were
correct, and I wasn't.

CC.


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

Date: Tue, 08 Jun 2010 18:01:05 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: convert integer to string
Message-Id: <kipt06hauiae9ghouv345ik1261n414eq7@4ax.com>

ccc31807 <cartercc@gmail.com> wrote:
>So, I guess my question is how I convert an integer to a string to
>preserve the leading zeros.

There _ARE_NO_ numbers with leading zeros.

A number (no matter if integer or whatever) is an abstract concept and
internally it is represented as whatever binary representation the Perl
interpreter chooses.

If you write down a number then that is no longer a number but a textual
representation, better known as a string.
And if you want to preserve the characteristics of that string, e.g.
leading zeros, then you need to treat that string as a string, not as a
number.

jue


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

Date: Tue, 08 Jun 2010 18:08:55 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: convert integer to string
Message-Id: <q7qt06hbc9rf1rj0bdefkq242ikak38amr@4ax.com>

ccc31807 <cartercc@gmail.com> wrote:
>I get some of the data in CSV format. One of my sources switched from
>an Access database to an Excel file. Turns out that Excel strips out
>the leading zeros if it thinks that the datum is an integer.

Which I would argue is the correct behaviour for a numerical data field.
If you don't want a canonical numerical form, then declare the data
field to be text. Problem solved.

jue


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

Date: Tue, 8 Jun 2010 20:14:09 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: How to read a given number of lines?
Message-Id: <slrni0t94h.bpb.willem@turtle.stack.nl>

Ben Morrow wrote:
) 'They' in this case being me? I wasn't trying to be clever, I was trying
) to write the code in a simple and obvious way. Counting indices by hand
) is much easier to get wrong than letting perl count them for you.

You mean like: for (0..2) { $lines[$_] = <IN> }

But in any case, this mistake is not a cleverness one,
because the exact same problem exists in the code:

 for (my $i = 0; $i < 3; $i++) {
   push @lines, <IN>;
 }

Which I would hardly call 'clever'.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


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

Date: Tue, 8 Jun 2010 13:58:14 -0700 (PDT)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: How to read a given number of lines?
Message-Id: <667a7acd-f703-41ac-8ddf-dbab1167bf6c@t10g2000yqg.googlegroups.com>

On Jun 8, 9:14=A0pm, Willem <wil...@turtle.stack.nl> wrote:

> You mean like: for (0..2) { $lines[$_] =3D <IN> }

I'am not smart enough to not try being clever.
Besides (and for wrong reasons: I used to like Lisp), I like maps.
So, I was thinking of the following minimal(?) fix(?):

my @lines =3D map scalar <IN>, 1..3;

Marc


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

Date: Tue, 8 Jun 2010 22:46:06 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How to read a given number of lines?
Message-Id: <u9q2e7-km52.ln1@osiris.mauzo.dyndns.org>


Quoth Willem <willem@turtle.stack.nl>:
> Ben Morrow wrote:
> ) 'They' in this case being me? I wasn't trying to be clever, I was trying
> ) to write the code in a simple and obvious way. Counting indices by hand
> ) is much easier to get wrong than letting perl count them for you.
> 
> You mean like: for (0..2) { $lines[$_] = <IN> }

Mmm, I suppose. I think my problem with that is that I just dislike
array indices other than [0] and [-1]: I think of arrays as 'frozen
lists', so either you iterate over the whole thing or you attack it from
the ends.

The original buggy code was essentially

    for (1..3) { $lines[$_] = <IN> }

and the use of explicit subscripts disguises the fact that there's an
extra 'undef' element on the beginning.

> But in any case, this mistake is not a cleverness one,
> because the exact same problem exists in the code:
> 
>  for (my $i = 0; $i < 3; $i++) {
>    push @lines, <IN>;
>  }
> 
> Which I would hardly call 'clever'.

I suspect that anyone approaching that code who didn't have a background
in C *would* call it clever, or at any rate much more complicated and
less comprehensible than a simple

    for (1..3)

Something like

    repeat (3) {...}

would be even simpler, of course, but unfortunately Perl doesn't allow
modules to create new syntax like that[0].

Ben

[0] ...without support code that will make you go blind.
    c.f. Devel::Declare, and/or source filters.



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

Date: Tue, 08 Jun 2010 16:40:54 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: stuff
Message-Id: <87iq5txnop.fsf@quad.sysarch.com>

>>>>> "R" == Robin  <robin1@cnsp.com> writes:

  R> I just wanted to say a few things about this newsgroup. I think it is
  R> a really good newsgroup. Even though, in the past there has been some
  R> things in this newsgroup I didn't like I also presenterd things that
  R> in the newsgroup people don't like. I am also hoping that people post
  R> stuff about more advanced perl stuff ethically and learn more perl
  R> ethically and people start learning some more advanced stuff
  R> ethically. I wanted to thank and or hope for the best thingsfor  this
  R> newsgroups in the future.

considering you spam this group which is unethical, i have to laugh at
you. hell, you aren't even worth a chortle.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 08 Jun 2010 17:23:15 -0400
From: Sherm Pendley <spamtrap@shermpendley.com>
Subject: Re: stuff
Message-Id: <m2hbldkym4.fsf@shermpendley.com>

Robin <robin1@cnsp.com> writes:

> I just wanted to say a few things about this newsgroup. I think it is
> a really good newsgroup.

So you feel compelled to ruin it with inane babble? Jerk.

*plonk*

sherm--

-- 
Sherm Pendley                <www.shermpendley.com>
                             <www.camelbones.org>
Cocoa Developer


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

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:

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

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 2980
***************************************


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