LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-11-2009, 05:22 AM   #1
abs_77
LQ Newbie
 
Registered: Feb 2007
Location: India, Bombay
Distribution: Linux & BSD
Posts: 23

Rep: Reputation: 15
Perl Variable help


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

Last edited by abs_77; 05-11-2009 at 05:24 AM.
 
Old 05-11-2009, 05:39 AM   #2
bhaslinux
Member
 
Registered: Oct 2003
Location: UnitedKingdom
Distribution: Debian Bullseye
Posts: 357

Rep: Reputation: 49
@newArray is array by itself (vector)
and $netArray[XX] is way of accessing the induvidual scalars inside array

so i would do it this way
use $newArray[xx] to save the reference to the line arrays
and then use @{$newArray[xx]} to get the induvidual array lines
 
Old 05-11-2009, 06:17 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by abs_77 View Post
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 ?

If yes, have you read

man perlreftut

?
 
Old 05-11-2009, 07:40 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
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."
 
Old 05-11-2009, 09:30 AM   #5
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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:
Code:
hektor ~ $ perl AoA
$VAR1 = [
          [
            'a',
            'b',
            'c',
            'd',
            'e'
          ],
          [
            'n',
            'o',
            'p',
            'q',
            'r'
          ],
          [
            'e',
            'r',
            'w',
            'd',
            't'
          ]
        ];
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";
That gives this:
Code:
Here's the first item in the first array: a
 
Old 05-11-2009, 09:45 AM   #6
abs_77
LQ Newbie
 
Registered: Feb 2007
Location: India, Bombay
Distribution: Linux & BSD
Posts: 23

Original Poster
Rep: Reputation: 15
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

Last edited by abs_77; 05-12-2009 at 03:53 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning a variable in perl aarontwc Programming 12 11-05-2008 06:44 PM
[Perl] wget in variable noir911 Programming 3 04-30-2007 06:39 AM
perl @INC variable nodger Programming 3 07-10-2006 11:17 PM
Perl Message board variable cadj Programming 3 04-21-2004 06:23 AM
question about $_ (default variable) in Perl realos Programming 4 07-04-2003 05:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration