Using arrays instaed of sequentially numbered variables

L

lance-news

Hey all,

Another question based on responses I received to another post
headed "concatenate variables during looping question". It was
recommended that I use a better data structure and that array(S)
would be a better choice for my structure then individual variables.

My basic question is how can I create arrays from the code below to
to create a better and simpler data structure?

Basically I open a Word Doc full of tables and have setup some regexp's
to grab numbers from these tables and then import them into excel.

These regexp'S are the same except for the middle string (i.e CLOTHEING, ITEMS ...)
and the variable names at the end.

My initial guess would be to create an array with the regexp search string
OVERALL RATING, CLOTHING ... and another array containing the results @JA13.
I have no idea how to set this up though?


###### JA ######
$file=~/(TABLE\s+36.*?TABLE 37)/s;
my $fileja=$1;
$fileja =~/TABLE\s+36.*?JA.*?OVERALL RATING.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;my $JA13_1 = $1;
$fileja =~/TABLE\s+36.*?JA.*?CLOTHING.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;my $JA13_2 = $1;
$fileja =~/TABLE\s+36.*?JA.*?ITEMS.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;my $JA13_3 = $1;
$fileja =~/TABLE\s+36.*?JA.*?ACCESSORIES.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;my $JA13_4 = $1;
$fileja =~/TABLE\s+36.*?JA.*?SHOES.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;my $JA13_5 = $1;
$fileja =~/TABLE\s+36.*?JA.*?FINE JEWELRY.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s; my $JA13_6 = $1;

###### KS ######
$file=~/(TABLE\s+37.*?TABLE 38)/s;
my $fileks=$1;
$fileks =~/TABLE\s+37.*?KS.*?OVERALL RATING.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;my $KS13_1 = $1;
$fileks =~/TABLE\s+37.*?KS.*?CLOTHING.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;my $KS13_2 = $1;
$fileks =~/TABLE\s+37.*?KS.*?ITEMS.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;my $KS13_3 = $1;
$fileks =~/TABLE\s+37.*?KS.*?ACCESSORIES.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;my $KS13_4 = $1;
$fileks =~/TABLE\s+37.*?KS.*?SHOES.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;my $KS13_5 = $1;
$fileks =~/TABLE\s+37.*?KS.*?FINE JEWELRY.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s; my $KS13_6 = $1;



# Answers from previous post
# Assign some values to each array

$Sheet->Cells(16, $i)->{Value} = $Ja13[$_ - 1] for 2 .. 7;
$Sheet->Cells(34, $i)->{Value} = $Ks13[$_ - 1] for 2 .. 7;

Again,

Thanks in advance

Lance
 
J

James Taylor

lance-news said:
My basic question is how can I create arrays from the code below
to create a better and simpler data structure? ....
These regexp'S are the same except for the middle string
(i.e CLOTHEING, ITEMS ...) and the variable names at the end.

You should factor the repeated bits out. Avoid repetition for the sake
of readibilty and maintainablilty.
My initial guess would be to create an array with the regexp search
string OVERALL RATING, CLOTHING ... and another array
containing the results @JA13.

Sounds like a good idea. Let's call the bits that are different the
"targets" and put them in an array:

my @targets = ('OVERALL RATING', 'CLOTHING', ITEMS',
'ACCESSORIES', 'SHOES', 'FINE JEWELRY');

Now you can use the map function and use $_ where the target string
would be and you only need to write the regex once:

my @JA13 = map {
$fileja =~
/TABLE\s+36.*?JA.*?$_.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;
} @targets;

Remember that array indexes start at zero, so the OVERALL RATING value
will be at $JA13[0] and CLOTHING value at $JA13[1], etc.
 
T

Tad McClellan

lance-news said:
Another question based on responses I received to another post
headed "concatenate variables during looping question".
My basic question is how can I create arrays from the code below to
to create a better and simpler data structure?

These regexp'S are the same except for the middle string (i.e CLOTHEING, ITEMS ...)
and the variable names at the end.

My initial guess would be to create an array with the regexp search string
OVERALL RATING, CLOTHING ...


A "list" will do, you don't need an "array".

and another array containing the results @JA13.
I have no idea how to set this up though?


###### JA ######
$file=~/(TABLE\s+36.*?TABLE 37)/s;
my $fileja=$1;


You should never use the "dollar digit" variables unless you have
first ensured that the match *succeeded*.

die "match failed" unless $file =~ /(TABLE\s+36.*?TABLE 37)/s;
my $fileja = $1;

$fileja =~/TABLE\s+36.*?JA.*?OVERALL RATING.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;
my $JA13_1 = $1;


The {1} doesn't do anything extra, so why put it in?

After you take it out, you won't need the non-capturing parenthesis either.

You probaby don't need the "prefix" part of that pattern either...

my $JA13_1 = $1;

$JA13[1] = $1;

or maybe:
push @JA13, $1;

$fileja =~/TABLE\s+36.*?JA.*?CLOTHING.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;my $JA13_2 = $1;
$fileja =~/TABLE\s+36.*?JA.*?ITEMS.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;my $JA13_3 = $1;
$fileja =~/TABLE\s+36.*?JA.*?ACCESSORIES.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;my $JA13_4 = $1;
$fileja =~/TABLE\s+36.*?JA.*?SHOES.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;my $JA13_5 = $1;
$fileja =~/TABLE\s+36.*?JA.*?FINE JEWELRY.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s; my $JA13_6 = $1;


foreach my $pat ('OVERALL RATING', 'CLOTHING', 'ITEMS') {
die "could not match '$pat'"
unless $fileja =~ /$pat.*?MEAN.*?(\d{1,3}\.\d\d)\s+/s;
push @JA13, $1;
}

# Answers from previous post


Not quite.

$Sheet->Cells(16, $i)->{Value} = $Ja13[$_ - 1] for 2 .. 7;
^^
^^

You should use copy/paste rather than retype code to avoid giving
yourself the opportunity to insert a typo like that.

Have you seen the Posting Guidelines that are posted here frequently yet?

(that was not a rhetorical question.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top