Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Using arrays instaed of sequentially numbered variables
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, ITE
MS ...)
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;m
y $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;m
y $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

Report this thread to moderator Post Follow-up to this message
Old Post
lance-news
01-27-06 12:00 AM


Re: Using arrays instaed of sequentially numbered variables
lance-news <lance-news@augustmail.com> wrote:

> 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.

--
James Taylor

Report this thread to moderator Post Follow-up to this message
Old Post
James Taylor
01-27-06 02:56 AM


Re: Using arrays instaed of sequentially numbered variables
lance-news <lance-news@augustmail.com> wrote:

> 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, I
TEMS ...)
> 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;  m
y $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.)


--
Tad McClellan                          SGML consulting
tadmc@augustmail.com                   Perl programming
Fort Worth, Texas

Report this thread to moderator Post Follow-up to this message
Old Post
Tad McClellan
01-27-06 11:59 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:31 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.