in reply to Working with arrays of variables

Here's another couple of things to add to the tips you have already received:

Your "desired" variables.txt output could and should omit the leading '$' from the variable names. There's no reason to have it in there, and you'll have to strip it after you read the file if you want to work easily with the contents. I suggest that your output file should be of the format:

cheese=cabrales tomato=orlando mass=secret

etc.

Since your file is in the format of an *.ini file, you should look into the Config::Tiny module (available on CPAN) which simplifies reading from and writing to *.ini-style files.

use strict; use warnings; use Config::Tiny; my $ini = Config::Tiny->new; my $outfile = 'pizza.ini'; my %data = ( 'cheese' => 'cabrales', 'tomato' => 'orlando', 'mass' => 'secret', 'USERNAME' => $ENV{'USERNAME'}, ); $ini->{'MySectionName'} = {}; # new section while( my ($key, $val) = each %data ) { $ini->{'MySectionName'}->{ $key } = $val; } $ini->write($outfile, 'utf8');

This outputs a file containing the following:

[MySectionName] USERNAME= cheese=cabrales mass=secret tomato=orlando

Note that the value for USERNAME is blank, because $ENV{'USERNAME'} does not exist on all platforms, e.g. on my Mac laptop. You should make this cross-platform.

Note that the data are contained in a hash; as others have said, you should convert your data to a hash or hashes before you do anything else. Post a followup question here if you need help with that.

Also: learn to use Data::Dumper whenever you are developing code with, um, data.

use strict; use warnings; use feature qw/ say /; use Data::Dumper; my %data = ( 'foo' => 'bar', $cheese => 'cabrales', 'USERNAME' => $ENV{'USERNAME'} ); say Dumper( %data );

If you run this, you will get an error because $cheese is not pre-declared. That's why you must use strict!!!

use strict; use warnings; use feature qw/ say /; use Data::Dumper; my %data = ( 'foo' => 'bar', '$cheese' => 'cabrales', 'USERNAME' => $ENV{'USERNAME'} ); say Dumper( \%data );

If you run this, you will see if the value of $data{'USERNAME'} is undefined ... before you pass it on.

Replies are listed 'Best First'.
Re^2: Working with arrays of variables
by perl_noobs (Initiate) on Jun 23, 2015 at 08:20 UTC

    First of all, thank you all for your time and your help!!!!!!.

    The above code with eval worked for us and we will search more about this method before using it (specially after reading about arbitrary code execution...) or perhaps export the Array contents to a "readable" format (XML, ini, etc.).

    for (my $i=0; $i<$n; $i++) { my $value = $pizzas[$i]; $value = eval $value if $value=~/^\$\w+$/; print $variables "$pizzas[$i]=$value\n"; }

    Perl is new for us, until a few months ago we were working only with VBS but we have been "gifted" with an obsolete Perl project (with "random" surprises as dirty code, few debugged, unuseful functions...).

    Our goal is, by first, that Perl works hand by hand with VBS and gradually migrating platform 100% to VBS (it's a boss decision, not ours, you know... hehe).

    Changing arrays to hashes would be possible, but also would be a long-term "mining" work because we have 300+ different "subprojects", with around 30 .pm each, calling functions from one to anothers (a very funny spiderweb), and one of these 30 .pm is containing the variables array that would be useful for us, those variables in the array could contain Windows environment variables ($ENV{USERNAME}...) (those scripts will only be executed under Windows plattform) or also the absolute path, UNC, strings, numeric, etc. And after changing them to hashes the hard work would be to adapt functions used in those scripts working with this "previous arrays", probably this effort won't be worth because one of these days (or years) the actual "Perl plattform" would be working 100% under VBS.

      Maybe you should suggest to your boss to hire a Perl programmer to migrate the data or the functions to a format you need. It sounds like that may be a lot cheaper as well as an order of magnitude faster, and would allow you and your team to get on with work you know how to do.