Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 18th, 2012, 02:57 PM
KuckFuggar KuckFuggar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 30 KuckFuggar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 20 m 50 sec
Reputation Power: 1
Data into 2d array

I have a text file with 2 columns and a bunch of rows. The columns are split by a whitespace. How do I use pure perl code and put it into a 2d array so i can accsess it like print $array[1][3]; So far my code does not split the columns it keeps it as one so if i do $array[1][0] i get the first 2 variables and if i do $array[0][1] i get an error.
Code:
use warnings;
#use strict;
use Data::Dumper;

my $file = 'green_scan.txt';
my @data;
my @array;
my $count = 0;
open (FH, $file) or die $!;
while ( <FH> ) {
	chomp($_);
	next if $. < 17;
	@data = <FH>;
	foreach $line (@data) {
		@array[$count++]=[split(/' '/, $line)];
	}
	print $array[5][0];

print "\n";
}
close FH;
exit 0;

Reply With Quote
  #2  
Old July 18th, 2012, 03:53 PM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,645 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 21 h 26 m 36 sec
Reputation Power: 1170
Code:
#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;

my $file = 'green_scan.txt';
my @data;

open my $fh, '<', $file or die "failed to open '$file' $!";

while (my $line = <$fh> ) {
	next if $. < 17;
        chomp $line;

	push @data, [ split ' ', $line ];
}
close $fh;

print Dumper \@data;
exit 0;

Reply With Quote
  #3  
Old July 19th, 2012, 11:38 AM
KuckFuggar KuckFuggar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 30 KuckFuggar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 20 m 50 sec
Reputation Power: 1
Quote:
Originally Posted by alb953wri
To use the Perl programming language you must install a Perl interpreter. Depending on the operating system you are using, it is possible that the interpreter may be already installed. To see whether it is installed, go to a command prompt and enter:

perl -v

If you get a message like 'command not found' or 'Bad command or file name', it means that the interpreter is not installed or the path of the interpreter has not been added to the system (environment) variable (in Windows).

If it is installed, its version number along with a few other bits of information will be printed (displayed on the screen).


what....

Reply With Quote
  #4  
Old July 19th, 2012, 11:53 AM
hostwindsEvanM hostwindsEvanM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 55 hostwindsEvanM User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 30 m 18 sec
Reputation Power: 1
Doesnt seem he reads very well.

Unfortunately, Im no help with Perl. However, depending on how you feed the data if its in an explicit format then it could easily be imported into the array.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Data into 2d array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap