ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am reading from a file and converting line to array for further processing. I am not able to create dynamic array variable for which each line be split and passed to the variable as array.
----------------------------------------------------
## READFILE goes like this ##
## a,b,c,d,e
## n,o,p,q,r
## e,r,w,d,t
while (<READFILE>) {
$arr[$i] = $_;
@newArray[$i] = split(/,/,$arr[$i]); ## This is not working
$i++;
}
#@newArray = split(/,/,$arr[$i]); #This is working fine
$arrLength = @arr;
print @newArray[$i];
---------------------------------------------------- Need help how to create dynamic array in this program.
I am reading from a file and converting line to array for further processing. I am not able to create dynamic array variable for which each line be split and passed to the variable as array.
----------------------------------------------------
## READFILE goes like this ##
## a,b,c,d,e
## n,o,p,q,r
## e,r,w,d,t
while (<READFILE>) {
$arr[$i] = $_;
@newArray[$i] = split(/,/,$arr[$i]); ## This is not working
$i++;
}
#@newArray = split(/,/,$arr[$i]); #This is working fine
$arrLength = @arr;
print @newArray[$i];
---------------------------------------------------- Need help how to create dynamic array in this program.
Thanks in Advance.
ABS
Do you want to have array of arrays, i.e. an array whose first index represents line numbers and whose second index represents element in the line number ?
Generally speaking, you need to first understand references.
Imagine, if you will, "data records floating out there in space," like kites, with one or more ropes tying them to the ground (and/or to other kites). Those ropes are references, and of course when the last rope is cut the kite simply "flies away" and disappears.
You can have lists, and arrays, and "hashes," either on the ground or mounted on other kites, as places to tie your ropes to.
The syntax if Perl is very "do what I mean," so it's not always obvious when and how references are being used, but they're basically "the way it's done."
If I understand you correctly, you want to end up with an array that has sub-arrays. Each sub-array should store the letters from a line of the text. As the others have said, you need references for this. For a little bit of background on why you need references and how to use them, I highly recommend that you take a look at perldoc perlreftut. You should also look at perldoc perllol and perldoc perldsc which are about building and using complex data structures in Perl.
As for this particular case, here's how I might do that. I'm assuming that the file is called READLINE:
Code:
#!/usr/bin/env perl
use strict;
use warnings;
open my $fh, '<', 'READFILE'
or die "Can't open 'READFILE' for reading: $!";
my @AoA;
while (<$fh>) {
chomp $_;
push @AoA, [ split /,/, $_ ];
}
close $fh or die "Can't close filehandle \$fh: $!";
use Data::Dumper;
print Dumper \@AoA;
The key bit is the second line in the while loop. What I'm doing there is pushing something onto the array. The odd thing is what I'm pushing. The split creates a list of items from the line of the file (splitting on commas - with more complex CSV files, you need a more serious solution, but we'll worry about that later). The square brackets take that list and make a reference to an anonymous array containing those letters. The anonymous array reference is what gets pushed onto the regular array @AoA (the name helps me to remember that it's an array of arrays).
The bit at the end with Data::Dumper (which is a core Perl module) is just a way to see what's happening while you program. It's useful for informal debugging. Here's the output:
That simply shows that I have an array inside of which are sub-arrays inside of which are the letters from the lines in the file. To operate on the individual sub-arrays, you need to dereference the items. So, for example:
Code:
print "Here's the first item in the first array: $AoA[0][0]\n";
Thanks to all you guys. I went through suggestion, explanation and code provided here and also gone through man for perlreftut, which i should have done before posting this.
-----Anyway this had worked for me.-----
while (<READFILE>) {
$arr[$i] = $_;
$newArray[$i] = $arr[$i];
$i++;
}
for ($i=0;$i<3;$i++) {
@{$newArr[$i]} = split(/,/,$newArray[$i]);
}
print "${$newArr[2]}[3]";
----------------------------------
Appreciate all of your help!!
Thanks,
ABS
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.