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:
Setting part of a string to an array

 



Turco
Deleted

Jan 26, 2001, 1:45 PM

Post #1 of 7 (904 views)
Setting part of a string to an array Can't Post

I was wondering how I'd take the first part of multiple strings in a file and assign them all to an array. The file looks like:

abc::234::wert::123
def::123::3456::gfhf
ect.....

and what I want to do is put everything up to the first instance of :: from each line in an array (ie: abc from the first line, def from the second, ect) How would I do this?

Thanks,

Jeremy



modified
Deleted

Jan 26, 2001, 1:50 PM

Post #2 of 7 (903 views)
Re: Setting part of a string to an array [In reply to] Can't Post


Code
open (FILE, "file.dat") || die $!; 
flock (FILE, 2);
@data = <FILE>;
close (FILE);

for ($x = 0; $x < @data; $x++) {
($item1[$x], $item2[$x], $item3[$x], $item4[$x]) = split(/::/, $data[$x]);
push (@array, $item1[$x]);
}

print "Content-Type: text/html\n\n";
print "@array";

Something like that.



Turco
Deleted

Jan 26, 2001, 2:08 PM

Post #3 of 7 (902 views)
Re: Setting part of a string to an array [In reply to] Can't Post

Thanks alot



modified
Deleted

Jan 26, 2001, 5:54 PM

Post #4 of 7 (898 views)
Re: Setting part of a string to an array [In reply to] Can't Post

Not a problem, did it work?



Cure
User

Jan 27, 2001, 9:02 AM

Post #5 of 7 (893 views)
Re: Setting part of a string to an array [In reply to] Can't Post

open CURE, 'file.txt' or die $!;
push @new,($_) = /^(.+?)::/ while (<CURE>);
close CURE;

print @new;


Cure



(This post was edited by Cure on Jan 27, 2001, 8:04 AM)


modified
Deleted

Jan 27, 2001, 1:00 PM

Post #6 of 7 (888 views)
Re: Setting part of a string to an array [In reply to] Can't Post

Cure, so the ^ in the regex means that it will only match the first one that it finds?



Cure
User

Jan 28, 2001, 10:21 AM

Post #7 of 7 (880 views)
Re: Setting part of a string to an array [In reply to] Can't Post

in the regex, the ^ means at the beginning of a string...

perldoc perlre


Cure


 
 


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

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