|
-
October 23rd, 2008, 05:16 AM
#1
how to create multiple array variable in a loop
In php code to create multiple array variable using loop and assign value will be as below
for($i=0;$i<10;$i++)
{
$j=$i+1;
$arr$j[$i]="some value";
}
Above code will create array variable from arr1 to arr10 and assign value.
I want to convert same code to perl , how can i do this
-
October 23rd, 2008, 07:09 AM
#2
Re: how to create multiple array variable in a loop
Did you do any research before posting? PHP and PERL are very, very close in syntax. In fact, in this example, the code would be identical. The only difference you might have is if use strict is declared. In that case, you would have to declare your variables before starting.
Your PHP code will error because you are missing brackets, and it will only produce one value per dimension.
Code:
for ($i = 0; $i < 10; $i++) {
$arr[$i + 1][$i] = "some value";
}
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
October 23rd, 2008, 08:08 AM
#3
Re: how to create multiple array variable in a loop
My actual code is as follows
#!D:\Perl\bin\perl
print "Content-type: text/html\n\n";
print "<b>Hello, world</b>";
$data_file="E:/LogAnalyzer/exim_mainlog.22";
open(FETs, $data_file) || die("Could not open file!");
@raw_data=<FETs>;
$j=0;
foreach $val (@raw_data)
{
@chars = split(/ /, $val);
for($i=0;$i<8;$i++)
{
$j=$i+1;
$arr$j[$i]=$chars[$i];
}
}
print @arr1;
print @arr2;
Here i want to retrieve line from file and split based on delimiter space and store each value in individual array inside the loop, so i want to create one dimensional array with varying array name, so i have added number along with the name $arr.
In the above case i have coded $arr$j[$j] to represent $arr1[0],$arr2[0]...
but the above code does not work
Last edited by kalaid; October 23rd, 2008 at 08:12 AM.
-
October 23rd, 2008, 09:06 AM
#4
Re: how to create multiple array variable in a loop
I am familiar with php, but I have never used perl
In this case though, you want to go with PeejAvery's suggestion, and use a multi-dimensional array.
you cannot use the $j variable in the way you describe above.
-
October 24th, 2008, 08:16 AM
#5
Re: how to create multiple array variable in a loop
What are you actually trying to do?
It looks like you want to parse each row of a log file that has 8 sections of data. The problem is with the way you've built this code you'll never get more than 8 rows of data containing 8 entries (after you fix the multi-dimensional array aspect).
I'm going to assume for simplicity's sake that the log file contains 8 columns of data per row (or at least you only care about the first 8), so the log would look something like this:
Code:
1 2 3 4 5 6 7 8
A B C D E F G H
.....
Now assuming again, that you want to create 8 separate arrays containing the values for each row, I would do something like this:
Code:
foreach $row (@raw_data)
{
@cols = split(/ /, $row);
for ($i = 0; $i < 8; $i++)
{
push @colarray[$i], $cols[$i];
}
}
for ($i = 0; $i < 8; $i++) { print @colarray[$i]; }
This would create 8 arrays, and for each row push the appropriate value on to the end of the array. When you're finished, you simply print each array via the index. This lets you read each row of @raw_data without limiting yourself to an 8x8 row of data (as was happening in the previous code.)
Ideally, you'd rename your variables a bit, and possibly even create named arrays / etc. It may take a bit more coding, but verbosity helps later when you're trying to read the code when your log file format inevitably changes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|