Re: Declaring variables recursively
by trammell (Priest) on Feb 25, 2005 at 15:20 UTC
|
| [reply] |
Re: Declaring variables recursively
by RazorbladeBidet (Friar) on Feb 25, 2005 at 14:56 UTC
|
#!/usr/bin/perl
my %variables = (
'one' => 1,
'two' => 2
);
${$_}=$variables{$_} for keys %variables;
$\="\n";
$,=" : ";
print $_, $$_ for keys %variables;
--------------
It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
| [reply] [d/l] |
|
|
Thanks it works, but well.Now that I see that you are really monks in perl I will show my real problem. It is the following(besides that Im not good programming!!):
I have a set of data like this:
-90 120
-50 40
120 -180
-50 90
-90 45
..., which are the (X,Y)coordinates of points in a 2d matrix.
The thing is that I would like to sort this data in such a way that the order shows how close these points are from each other. The real objective behind it to make a contour plot from this points. So I have already the shape(points) in the 2d plot but now I want to connect them and have the contour. In fact I realised that regular programs draw lines between points in order of appeareance. So I got to the point of having to order them appropiately( or according to me by proximity).
What do you suggest monks?
-Arnold
| [reply] |
|
|
| [reply] |
|
|
I'm not entirely positive I understand what you need here. You want the number sorted based on how close they are to... one another, or a central point on the grid? If you want to draw a line connecting them, it sounds like you need to start at one side, and go directly across the plane (left to right for simplicity sake). If that is the case you would want the list sorted based on the X cooridante in each pair. Beacuse these are pairs of numbers, it sounds like you should have an array of arrays (two dimensional array). To sort a two dimensional array based of the first of the two elements in the inner arrays:
my(@sorted_array) = sort({$a->[0] <=> $b->[0]} @unsorted_array);
See sort for details on using sort. See perlre for details on nested data structures.
May the Force be with you
| [reply] [d/l] |
|
|
| [reply] |
|
|
Re: Declaring variables recursively
by Fletch (Bishop) on Feb 25, 2005 at 15:03 UTC
|
Possible, yes. Good idea, probably not. (Like Yoda talking why am I . . .)
If you're trying to do something like this chances are you really want a data structure like a hash of hashes or some such (see perldoc perldsc, perldoc perllol).
| [reply] [d/l] [select] |
Re: Declaring variables recursively
by blazar (Canon) on Feb 25, 2005 at 15:28 UTC
|
I would like to declare variables recursively, this way I could save lots of time in associating data of arrays hashes etc. What I would like to to is the following: foreach $name (@array){ ${$name}=X } is it possible con perl?, because its posible with linux shell. Thanks
- yes, it's possible, and it fundamentally amounts to accessing the keys of one particular, special hash, namely the symbol table. These animals are called "symbolic references" (or more shortly, "symrefs").
- since you're manipulating a hash, just use a "real", generic one:
$hash{$_} = X for @array;
takes only a few more keystrokes... (well, actually less, but that's only because I used for as a statement modifier.)
In particular there are tons of mails/posts/nodes/articles explaining why it is generally recommended not to use symrefs, which is also the reson why they're ruled out by use strict; (hey, you're using it, aren't you?)
- WRT "is it possible con perl": in inglese dovresti usare "with" ;-)
| [reply] [d/l] [select] |
|
|
>>3.WRT "is it possible con perl": in inglese dovresti usare "with" ;-)
>>>> Right, but also in spanish :)-my mother tongue
| [reply] |
Re: Declaring variables recursively
by perlfan (Parson) on Feb 25, 2005 at 16:55 UTC
|
If you want to do contour plots and are dealing with real data, you can't go wrong with PDL (perl data language) | [reply] |
Re: Definings variables recursively
by chas (Priest) on Feb 25, 2005 at 15:14 UTC
|
You can definitely do that; you are using soft references in creating the variables $$name (note that you don't even need the extra {}.) Sometimes this is frowned upon, but in a small
program it seems OK. (I've done that to create variables from form input in a cgi.)
chas | [reply] |
|
|
Creating/defining variables from input from CGI is precicely, and especially one of the things one shouldn't do. What if I'm really careful? (offsite) describes pretty effectively why it's a terrible idea to accept input of any kind from a CGI script, that is used to create a variable name via a symbolic reference. This document is part of a three-part collection on the subject, the links to which are included at the end of the doc.
The point is that if you think your method is safe, it's probably because you've overlooked the real danger.
| [reply] |
|
|
Dave,
Thanks for your comments. I definitely agree. Actually, I've done the following:
....
@varnames=('name','affiliation','email','radio','geometric');
for (@varnames){$$_=param($_)};
....
So, I've only made variables out of some strings that I have
chosen myself. But this certainly isn't necessary, and I am going to rethink this.
chas | [reply] [d/l] |