Re: Clear a hash
I think you want %hash = (); which will make sure there are no elements in %hash. Your statement was %hash = {}; In this case, the {} will return a reference to an empty hash, and the assignment will then attempt to make this reference into a key in %hash. Since hash keys can only be strings, not references, it will turn this reference into a string like HASH(0x18015d8) (the number will vary depending on the memory location in your particular copy of perl). So %hash = {} is basically equivalent to %hash = (HASH(0x18015d8), undef). This is probably not what you wanted. On Jan 18, 2008, at 8:57 AM, Kevin Viel wrote: Is there a way to empty/clear a hash in mass? For instance: %hash = {} ; Might the above create an reference? Thank you, Kevin Kevin Viel, PhD Post-doctoral fellow Department of Genetics Southwest Foundation for Biomedical Research San Antonio, TX 78227 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ -- Aaron Priven, [EMAIL PROTECTED],com, http://www.priven.com/aaron
Re: Variable division, assignment and sprintf in one line
On Nov 28, 2007, at 11:26 AM, Steve Bertrand wrote: Is there a way that one can use sprintf on a variable and assign the value back to the variable without having the leading $var= ? I don't think you can do it directly, but you can use the aliasing properties of sub or for: $a_really_long_variable_name = 100*1024*1024; print $a_really_long_variable_name\n; divide($a_really_long_variable_name); print $a_really_long_variable_name\n; sub divide { # ha ha I love this pun $_[0] = sprintf (%.2f, (($_[0] /=1024) /=1024)); } or $a_really_long_variable_name = 100*1024*1024; print $a_really_long_variable_name\n; $_ = sprintf (%.2f, (($_ /=1024) /=1024)) for $a_really_long_variable_name; print $a_really_long_variable_name\n; Both give the same output: 104857600 100.00 The latter is most useful if you really are iterating over a list, of course. -- Aaron Priven, [EMAIL PROTECTED],com, http://www.priven.com/aaron
dereferencing
I can do this: my $hashref = \%hash; But as near as I can tell, there is no way to do the reverse operation: making a my %hash that is an alias for a hash reference. It's possible with a package variable: #/usr/bin/perl our %hash; my $hashref = { a = 5 , b = 8 }; *hash = $hashref; print $hash{a}; that prints 5. But there's no way to do this with a lexical (my) variable. is this right? -- Aaron Priven, [EMAIL PROTECTED],com, http://www.priven.com/aaron
Re: dereferencing
On Oct 29, 2007, at 5:21 PM, Tom Phoenix wrote: I don't think that what the original poster wants is even possible, but I can't see any reason to need it, either. It's probably not necessary per se, as you can always do anything using reference syntax, but it's awkward and requires lots of extra dereferencing by the interpreter. If %hash has a few thousand entries, routine (%hash); sub routine { my %myhash = @_; ... } is wasteful and timeconsuming, and I don't want to have to type lots of extra arrows just because I passed the hash to a subroutine. And now I don't have to. Yay. -- Aaron Priven, [EMAIL PROTECTED], http://www.priven.com/aaron -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: dereferencing
Playing around with this, indeed, proves it can be done: #!/usr/bin/perl use strict; use warnings; use Data::Alias; my $hashref = { a = 5, b = 8}; alias my %hash = %{$hashref}; print $hash{a} , \n; prints 5. (And \%hash == $hashref evaluates true.) I knew that Data::Alias existed -- it's mentioned in Perl Best Practices -- but at least to me, the documentation seemed to imply that it couldn't do this, or that it would act like many scalar assignments in parallel. Thanks! On Oct 29, 2007, at 5:33 PM, Paul Johnson wrote: Perhaps something like Data::Alias, for example, might do the trick. -- Aaron Priven, [EMAIL PROTECTED], http://www.priven.com/aaron -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Bad scoping? Bad prototyping?
On Oct 9, 2006, at 10:35 AM, Helliwell, Kim wrote: #!/bin/perl sub1(Hello, ); sub1(world\n); sub sub2($str) { print $str; } sub sub1($str) { sub2($str) } Prototyping in perl does not do what you think it does. It does not turn your arguments into variables. All it does is supply a scalar or list context to entries in your argument list. It is not a useful feature for most people most of the time, and should be avoided. http://library.n0i.net/programming/perl/articles/fm_prototypes/ Arguments to subs are always in the array @_ #!/bin/perl use warnings; use strict; # no Top::Posting; sub1('Hello, '); sub1(world\n); sub sub2 { my $str = shift; # implicitly uses @_ print $str; } sub sub1 { my $str = shift; sub2($str) } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Polluting the Global Namespace
See perldoc Exporter. If you use @EXPORT instead of @EXPORT_OK, it will put those subroutines into the global namespace by default. On Jul 20, 2006, at 3:09 AM, Shane Calimlim wrote: I'd like to add some sub routines to the global namespace. I know this is usually considered bad design for modules, but I intend to do this only for a personal project. I'd just like to know if this is possible, and how. And if so, using pure perl code or would I have to delve into xs? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: how to add a list of numbers
Well, you can add a list of numbers with the module List::Util . It has a routine called sum. my @list = (8,10,2,5); use List::Util qw(sum); my $sum = sum (@list); # $sum is now 25 It can add any numbers, random or not. On Jul 14, 2006, at 11:00 PM, I BioKid wrote: is there any shortest way to add a list of random numbers ? for example : 11 1 250 39 100 , thanks -- ibiokid -- Aaron Priven, [EMAIL PROTECTED], http://www.priven.com/aaron -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: math formula substitution and evaluation
On Jul 15, 2006, at 5:15 PM, Daniel D Jones wrote: Given something like the following: my @variables = [3, 7, 13, 4, 12]; As an aside, you meant parentheses here, not brackets. (Brackets return a reference to an anonymous array containing the list, not the list itself.) my @tests = (2*a+b==c, c-d+a==e); I need to be able to evaluate the mathematical truth of the tests, using the values from @variables, where $variable[0] holds the value of the variable 'a', $variables[1] holds the value of 'b', etc. I can do the evaluation but how do I (reasonably efficiently) substitute the values into the strings? (The length of the array and the exact tests are not known until run time.) In C, I'd do this by walking through the test strings character by character, checking for isalpha, converting the character to its ascii value and subtracting the ascii value of 'a', and indexing into the array. This should be easy enough to do, using the length, ord, chr, and substr functions (I haven't tested this): foreach my $test (@tests) { for my $charposition (reverse (0 .. length($test))) { # go through the string backwards so your count isn't messed up by adding # more characters than you took away my $char = substr($test, $charposition, 1); next if $char !~ /[[:alpha:]]/; my $variableindex = ord(lc($char)) - ord(a); substr($test, $charposition, 1, $variables[$variableindex]); } } But it's not a very perl-y way of doing this. In Perl I'm not sure how to do this type of low level character by character processing. The only thing which occurs to me is to split() the string into an array of characters, then walk through that array. Am I on the right track or is there an easier way to do this in Perl? If the tests really do look like what you've displayed, then the easiest way would be to make sure your tests are proper perl code, and then run them through eval. Unlike C, Perl has an entire parser available at runtime for you to use, so there's no need to write your own. So you'd do something like (also untested): my ($a, $b, $c, $d, $e) = (3,7,13,4,12); my @tests = (2*$a+$b==$c, $c-$d+$a==$e); foreach my $test (@tests) { if (eval($test)) { print $test is true!\n; } } This does mean you have to reformat the tests to be valid perl. An alternative would be (again, untested) my %value_of = ( a = 3, b = 7, c = 13, d = 4, e = 12); foreach my $test (@tests) { foreach my $variable (keys %value_of) { my $value = '$value_of{' . $variable . '}'; # the above is in single quotes, so will be evaluated during # the eval, not during the substitution below $test =~ s/$variable/$value/g; } } foreach my $test (@tests) { if (eval($test)) { print $test is true!\n; } } Either way, using eval is potentially be a security issue since if these are user-supplied tests, then the user can run arbitrary code this way. Fine in some circumstances, bad in others. -- Aaron Priven, [EMAIL PROTECTED], http://www.priven.com/aaron -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: How to get the OS local/default drive using perl?
For a temporary file, you'd want to use the File::Temp module (see http://search.cpan.org/~tjenness/File-Temp-0.16/Temp.pm ). For a permanent file, such as a configuration file, you'd probably have to figure it out for each operating system, and then test $^O to figure out which operating system you're running under. Each OS is different in where different kinds of files should be stored. On Jul 13, 2006, at 12:40 PM, Nishi Bhonsle wrote: Hi: If I have to create a writeable file say write.txt on fly during program execution under the OS default/local drive of a machine ie C:\ or D: \ on windows and /home/user etc on unix, how can I get that local/ default drive using perl so that I dont have to hardcode C:\ or D:\ or /home etc inside the script as path to the write.txt. Thanks much. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: regex repetition
Check out the module Set::IntSpan and see if it does what you want. http://search.cpan.org/dist/Set-IntSpan/IntSpan.pm On Jul 12, 2006, at 3:08 PM, Ryan Moszynski wrote: I need to write some code to allow users to specify which of a whole bunch of elements(e.g.512/1024) that they want to view. My idea for how to do this was to have them input a semicolon delimited list, for example: 1-10;25;33;100-250 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
... is a valid filename
For what it's worth, ... is indeed a valid filename. Back in Ancient Days of Yore, when I was a young undergrad at UC Santa Cruz playing on the open-access timeshare Unix system, we all had read access to each others' home directories, and it was somewhat common for people to put semi-secret stuff in a directory ... and mark it readable/searchable by the user only. The name gave no indication of what was actually in there, and if you did ls -a, it was easily overlooked next to . and .., which is a really pathetic form of security by obscurity but better than nothing, especially in 1989. So unless you have made a decision that you want to exclude files or directories that are ... and and ., you need to not just block everything that's made up entirely of periods. Somebody sent in something just now saying that the regular expression posted had to start with a period. That's not true. The expression posted was [^.] Because it's in brackets, this is a character class. See Using character classes in the perlretut manpage (perldoc perlretut). Within a character class, ^ doesn't mean start, it means negated character class. From perlretut: The special character ^ in the first position of a character class denotes a negated character class, which matches any character but those in the brackets. And, within a character class, . doesn't have its other meaning for any character but newline (unless you've used the s modifier, in which case it matches that too), but instead is an ordinary character signifying itself. So [^.] matches a string that has at least one character that's not a period in it. .abc abc.def abc abc all match because the character class finds the first a in each string, and that's a character other than a period. But ... does not match, because the character class can't find anything that's not a period in it. Similarly, . and .. won't match. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: passing by value vs. passing by reference
You should show us some code. Normally you would do this in the subroutine: sub routine { my $mydir = shift; # which puts the value of $_[0] into $mydir # and then removes that from the argument list chop $mydir; # do stuff with $mydir } That makes a copy of the value. It sounds like you are doing this: sub routine { chop $_[0]; # do stuff with $_[0] } which is harder to understand, since $_[0] has no meaning for anyone, and has the disadvantage of changing your original value. If for some reason you sometimes need to pass the original variable and sometimes pass a copy of the value to your subroutine, you could pass it an expression that returns the value of $a. There's probably a good way I can't think of to do this in all circumstances, but if it's a string you could call it with routine($a); or if it's a number, routine($a + 0); By the way, you should look at chomp and see if it's more like what you want than chop. Maybe it is; maybe it isn't. On Jul 4, 2006, at 10:17 AM, [EMAIL PROTECTED] wrote: I have a subroutine that, amongst other things, chops a scalar variable, $dir, passed to it as an argument. The problem is that I need $dir intact (ie unchopped) after calling said subroutine, but it has been altered by the chop. -- Aaron Priven, [EMAIL PROTECTED], http://www.priven.com/aaron -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: passing by value vs. passing by reference
On Jul 4, 2006, at 10:46 AM, Aaron Priven wrote: you could pass it an expression that returns the value of $a. Sorry, I should have said your variable instead of $a here. -- Aaron Priven, [EMAIL PROTECTED], http://www.priven.com/aaron -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response