From: Martijn Lievaart on 8 Jun 2010 17:49 On Tue, 08 Jun 2010 14:12:53 -0700, ccc31807 wrote: > On Jun 8, 4:40 pm, "Uri Guttman" <u...(a)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
From: Ben Morrow on 8 Jun 2010 17:52 Quoth "Uri Guttman" <uri(a)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
From: Tad McClellan on 8 Jun 2010 20:02 ccc31807 <cartercc(a)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.
From: ccc31807 on 8 Jun 2010 21:00 On Jun 8, 5:47 pm, Tad McClellan <ta...(a)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.
From: J�rgen Exner on 8 Jun 2010 21:01
ccc31807 <cartercc(a)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 |