CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Splitting into an array

 



JonnyThunder
User

Jun 25, 2002, 1:47 AM

Post #1 of 9 (1269 views)
Splitting into an array Can't Post

HI,

I've got a file which I've opened and read into an array called @member. The file contains info which is seperated with || (double pipe). Say there are three bits of data for each line in the file A, B and C. How can I split these lines into an array so I can use them later?

Thanks,
JT

example of data file....

user1||testdata||testdata2
user2||againmoredata||moredatahere
user3||blahblah||finalbit


davorg
Thaumaturge / Moderator

Jun 25, 2002, 5:36 AM

Post #2 of 9 (1266 views)
Re: [JonnyThunder] Splitting into an array [In reply to] Can't Post

You need the "split" function. See "perldoc -f split" or http://www.perldoc.com/perl5.6.1/pod/func/split.html.

In your case, beware that the first argument to "split" is a regular expression and the pipe character is a regex metacharacter. You'll need to escape it like this:

Code
my @vals = split /\|\|/, $data;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


JonnyThunder
User

Jun 25, 2002, 6:17 AM

Post #3 of 9 (1264 views)
Re: [davorg] Splitting into an array [In reply to] Can't Post

I'm not sure I understand... you have to speak slowly for me! :)

This line only splits for one entry in my file doesn't it? In my example, if I wanted to split each line in my file, into ... say @a, @b and @c... how would I go about it? So that they would hold data like this...

$a(1) = user1
$a(2) = user2
$b(1) = testdate
$b(2) = againmoredata

etc.


Sorry. I'm thick and need explanation! :)

p.s. Sorry bout the private reply... was an accident!


(This post was edited by JonnyThunder on Jun 25, 2002, 6:19 AM)


JonnyThunder
User

Jun 25, 2002, 6:29 AM

Post #4 of 9 (1261 views)
Re: [JonnyThunder] Splitting into an array [In reply to] Can't Post

OK. I've done this in order to split my data up per item. (@Member holds data read in from file)...


Code
foreach $dataitem (@member) 
{
($a,$b,$c) = split(/\|\|/, $dataitem);
}


For each of the lines in my file I can split it into it's seperate components. How can I put these back into an array. For instance, I want to be able to read $a[number] to get a specific piece of data.

Does this make sense?

TIA,
JT


davorg
Thaumaturge / Moderator

Jun 25, 2002, 6:57 AM

Post #5 of 9 (1258 views)
Re: [JonnyThunder] Splitting into an array [In reply to] Can't Post

Sounds like what you actually want is a array of arrays. Take a look at the perllol, perlreftut and perldsc manual pages for more help.

Code
my @split_data; 
foreach (@member) {
push @split_data, [ split /\|\|/ ];
}

You then have all of your data in @split_data. You can reference each data item individually:

Code
my $item = @split_data[0][0]; # the first item in the first record

or a record at a time:

Code
my @data = @{$split_data[1]}; # the whole of the second record


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


JonnyThunder
User

Jun 25, 2002, 7:25 AM

Post #6 of 9 (1257 views)
Re: [davorg] Splitting into an array [In reply to] Can't Post

Ahhh. Interesting. Getting closer all the time!

Thanks for the help. I shall give this a shot (and read more of the manual too!)

:)

Thanks again. Expect more stupid questions in the future.


davorg
Thaumaturge / Moderator

Jun 25, 2002, 7:43 AM

Post #7 of 9 (1255 views)
Re: [JonnyThunder] Splitting into an array [In reply to] Can't Post


In Reply To
Thanks again. Expect more stupid questions in the future.

I look forward to them :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


JonnyThunder
User

Jun 25, 2002, 7:50 AM

Post #8 of 9 (1254 views)
Re: [davorg] Splitting into an array [In reply to] Can't Post

 
Didn't think it would be this soon tho eh?

Tried that out. Perl editor is complaining about the [0][0] part of it. Says it'd be better written as [0] - (which doesn't work!).

Any ideas?


davorg
Thaumaturge / Moderator

Jun 25, 2002, 8:10 AM

Post #9 of 9 (1253 views)
Re: [JonnyThunder] Splitting into an array [In reply to] Can't Post

Oops. That code should be:

Code
my $item = $split_data[0][0]; # the first item in the first record


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives