LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-04-2006, 11:22 PM   #1
wwnexc
Member
 
Registered: Sep 2005
Location: California
Distribution: Slackware & Debian
Posts: 264

Rep: Reputation: 30
Post Perl Script: Variable Names composed of Variables


Hi,

I am pretty new to perl, and i have just begun writing a small sudoku solver.

I am wondering if there is a function which allows you so combine two variables and use them as the name for an other variable.

For example:
$i=55;
$n=12;
$i,$n="whatever the variable should contain";

The $i,$n= are supposed to be 5512. Is there any way to do such a thing? Or is there a way to get 3 dimensional matrices or other arrays with more than one dimension? If there are such things, what do they look like??

Thanks!!
 
Old 02-05-2006, 05:11 AM   #2
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 77
Quote:
Or is there a way to get 3 dimensional matrices or other arrays with more than one dimension? If there are such things, what do they look like??
Sure. You can create a hash of arrays:
Code:
%hash_of_array = (
foo => [1, 2, 3],
bar => [4, 5, 6],
baz => [7, 8, 9],
);
or even hashes of hashes:
Code:
%hash_of_hash = (
foo => {
   x => 1,
   y => 2,
   },
bar => {
   x => 3,
   y => 4,
   },
);
You can pretty much nest any structure into any other (except simple scalars) to any arbitrary depth.
 
Old 02-05-2006, 07:34 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,312

Rep: Reputation: 175Reputation: 175
you can't have a comma, in a perl variable name and you can't start with a digit.
You should use bullivers suggestion.

However FYI:
You can do things like this though:

Code:
$ perl
$x="hello";
$$x = "goodbye";
print "$x, $hello\n";
hello, goodbye
combining two would need an intermediate step...

Code:
$ perl
$i=a55;
$n=b12;
$x="${i}_$n";
$$x = "combined";
print $a55_b12;
combined$
though I would say this is horrible and ugly.
Also it is a whole world of bugs waiting to happen!
 
Old 02-05-2006, 11:58 AM   #4
wwnexc
Member
 
Registered: Sep 2005
Location: California
Distribution: Slackware & Debian
Posts: 264

Original Poster
Rep: Reputation: 30
Thanks for all your help.

I went with bulliver's method. I now am able to store each possible value in a list, which of each field corresponds to a list of possibilities.

Bigbearsbilly, you are right - it works, but it's only bugs waiting to happen. I am probably not going to use this method, even thoug it looks interesting.
 
Old 02-06-2006, 04:47 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,312

Rep: Reputation: 175Reputation: 175
You can do it with funcs. Can be very useful.
E.g. I was writing a server and wanted a list of valid commands and help text for each
but also it's easy to lose track. So I did:

Code:
@command_list = sort qw( DUMMY DUMP ADD DEL WHO DROP LOAD COUNT QUIT HELP );
foreach my $c (@command_list) {

    unless (defined &$c) {
        warn "WARNING:command $c not defined!" ;
        *$c = sub { print OUT "?function $c?:not defined" };
    }
so you get a dummy command defined with a warning.

Last edited by bigearsbilly; 02-06-2006 at 09:46 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 11:22 AM
What are the names of the variables passed to konqueror's email client? BluePyre Linux - Software 0 07-04-2004 12:33 PM
variable names in your code (fun) titanium_geek Programming 12 12-22-2003 11:12 AM
Variable names that include variables.... mychl Programming 7 09-24-2003 08:13 AM
Perl question re arrays/variable names Jon- Linux - Software 1 09-19-2003 11:29 AM


All times are GMT -5. The time now is 12:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration