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!

concat array slice to variable name 1

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
This question piggy backs on my "getting a hash key based on value" thread.

Now that I have my proper month numbers and text in arrays (Thanks PaulTEG [medal]), I want to somehow concatinate the array slices to a variable name so I can increment it (count instances of it) later in my program. So here is my swipe at it. For all I know it may work, but if you guys have any thoughts........

Code:
#
#  Get the months for the 4 month chart
#
$curr=$rpt_month;
my @text	=("January", "February", "March",
				"April",   "May",      "June",
				"July",    "August",   "September",
				"October", "November", "December");
my @months	=('01','02','03','04',
				'05','06','07','08',
				'09','10','11','12');
my @last4months=($months[$curr-4],$months[$curr-3],$months[$curr-2],$months[$curr-1]);
my @last4text  =($text[$curr-4],$text[$curr-3],$text[$curr-2],$text[$curr-1]);
print (LOG "Months for 4 month chart: @last4text\n\n");
my ("$close_cnt_" . "$last4months[0]") = 0;
my ("$close_cnt_" . "$last4months[1]") = 0;
my ("$close_cnt_" . "$last4months[2]") = 0;
my ("$close_cnt_" . "$last4months[3]") = 0;
my ("$open_cnt_" . "$last4months[0]") = 0;
my ("$open_cnt_" . "$last4months[1]") = 0;
my ("$open_cnt_" . "$last4months[2]") = 0;
my ("$open_cnt_" . "$last4months[3]") = 0;

then I am going to try to use it later as in:

Code:
("$close_cnt_" . "$last4months[0]")++

or maybe

Code:
("$close_cnt_$last4months[0]")++

Am I smoking something here thinking it will work?
 
you probably can but why not start with a list to make it easier to get all the results later on, something like:

Code:
my %closed_cnt = ();
my %opend_cnt = ();

$closed_cnt{$last4months[0]}++;
$opened_cnt{$last4months[0]}++;
etc
etc
etc
 
Nick,

Now you need hashes,
Code:
my %open;
my %closed;
foreach (@last4months) {
  $open{$_}   = 0;
  $closed{$_} = 0;
}
later
Code:
$open($last4months[0]}++;

HTH
--Paul

cigless ...
 
Thanks Kevin...that could work. I'll try it and let you know.

Nick
 
I was just looking at this code:
Code:
@last4months=($months[$curr-4],$months[$curr-3],$months[$curr-2],$months[$curr-1]);
my @last4text  = ($text[$curr-4],$text[$curr-3],$text[$curr-2],$text[$curr-1]);
That's going to break if the current month is before May (since there won't *be* 4 previous months in your array). If you're doing date calculations like this, you'd be better off using a properly robust date-manipulation module such as Date::Calc
 
It wraps around, I tested it for January, just one o'dem things

--Paul

cigless ...
 
It seems to work fine ishnid. I tested it for months before May.
 
>> It wraps around, I tested it for January, just one o'dem things
>> It seems to work fine ishnid. I tested it for months before May.

Indeed it does. Seems like I've been looking at too much Java and not enough Perl recently :(
 
circular lists implemented by default, I remember having to code this cr@p in COBOL, and Pascal

Perl is gr888
--Paul

cigless ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top