Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I name variables after a list?

Status
Not open for further replies.

DazzerTperl

Programmer
Joined
Nov 1, 2006
Messages
7
Location
GB
I have a list of variables in an array and I want to effectively create an array for each of these named headings.

ie list is say name, title, location and I want to call the variable array $[name][$i], $[location][$i], $[title][$i], and fill them via a loop $[name][$i]=whatever.

I know its simple but I can't remember how to.

Please jog my memory.

Dazzer
 
Use a hash of array refs.

Code:
@array = qw(name location title foo bar baz);

my %data = map {$_ => []} @array;

push @{$data{name}}, 'name1';
push @{$data{title}}, 'title1';
..etc
 
Thanks Miller, that looks extremely useful, but I'm not sure I explained myself clearly.

I'm sure there's a way to scroll through an array

$i=0;
foreach (@array) {
$new_array_{$i}[$j] =$array[$i];
$i++;
}

where the variable $new_array_{$i}[$j] becomes $new_array_7[$j] if $i=7;

I'm trying to separate the data in around 30 columns of mysql, without having to write each variable definition line, all thirty of then, in turn.

If that's what your code does, I'm even more appreciative I'm just not familiar with push and % refs yet.

thanks,

Dazzer
 
Perl documentation:


DazzerTperl said:
where the variable $new_array_{$i}[$j] becomes $new_array_7[$j] if $i=7;

I'm trying to separate the data in around 30 columns of mysql, without having to write each variable definition line, all thirty of then, in turn.

My code was simply demonstrating how you should not try to create a new variable (ie $new_array_7[$j]) for each column. Instead, just maintain one massive data structure with all of your data. You can do this by having a hash with keys being the names of your columns, and the values being arrays full of the data. You can do it using multidimensional arrays, with the first layer being indexed matching the column indexes.

Your attempt to munge variable names to $new_array_7 is not a good idea. It is technically possible, but highly advanced, and just bad. You never should do this unless there is a specific reason why another data structure won't work.

Code:
my @columns = qw(name location title);
my %indexOfColumn = map {$columns[$_] => $_} (0..$#columns);

my @table;
while (@record = $dbh->fetchrow_array) {
    for (my $i = 0; $i <= $#record; $i++) {
        push @{$table[$i]}, $record[$i];
    }
}

# Print out all names in order.
print join "\n", @{$table[$indexOfColumn{name}]}.

For example.
 
thanks I see your point now - the steer and advice is much appreciated. I must really get my head round multidimensional arrays with keys!

Dazzer
 
MillerH

I've tried using your code but oddly I get the error 'Can't locate object method "fetchrow_array" via package "DBI::db"'

Have I forgetten to define something? Or do I need another package for DBI?

Dazzer
 
DazzerTperl said:
I've tried using your code but oddly I get the error 'Can't locate object method "fetchrow_array" via package "DBI::db"'

Have I forgetten to define something? Or do I need another package for DBI?

My code was only meant to be used as a model. I do not know what module or method you are using to access your database. I simply employed one of the methods that I commonly use as an example.

The overall code and data structure design should work for your problem. But you'll have to adapt your currently existing code to my snippet. Which obviously will require that you learn what it is doing hopefully :).

Advanced Data Structures can be very challenging for the uninitiated, but you'll find that they are extremely powerful and very easy to do in perl. It's just going to take some time to get more familiar with them most likely.

If you share whatever code you have so far for ...

DazzerTperl said:
I'm trying to separate the data in around 30 columns of mysql, without having to write each variable definition line, all thirty of then, in turn.

... then someone might be able to help you. But I suggest that you give it a go yourself first and only ask once you run into a problem.

I'm going to bed now myself, so I won't be around to answer any more questions for a few hours. Good luck.
 
When you find yourself wanting to put numbers into variable names to count them, your first reaction should be "What am I thinking - that should be an array". It's really just a change to your way of thinking that should be all you'll need.

If you need further persuasion that using variables in variable names is a "bad thing", have a look here.
 
Thanks, MillerH, I've got your code working but it'll only print column data [title] for column one [name] and column four [col_four] for column two [location] - very odd. It must be double counting somewhere but I can't see where.
 
Thanks again MillerH - ignore my previous question - I wasn't reading the db I thought I was - duh!

I've got it to work - it's still a bit long hand.

How can I replace "name" below with columns[0] in:

@{$table[$indexOfColumn{name}]}

I guess that involves a radical rewrite. It's just so I can itterate through columns array printing its related table data --- without having to do this:

@name=@{$table[$indexOfColumn{name}]};
@title=@{$table[$indexOfColumn{title}]};
@date=@{$table[$indexOfColumn{date}]};
@version=@{$table[$indexOfColumn{version}]};
@genre=@{$table[$indexOfColumn{genre}]};
@track=@{$table[$indexOfColumn{track}]};

and so on

thanks again,

Dazzer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top