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:
Arrays

 



waherne
Novice

Jan 11, 2001, 8:59 AM

Post #1 of 4 (830 views)
Arrays Can't Post

Hi all,

Can anyone help me to do this?

data.dat contains:

a|b|c|d|e
f|g|h|i|j

If data.dat is opened as an array, how does one identity the variables?

For instance, how do I to pull line 1 item 3 and line 2 item 4, join them together and create "ci"?

Willie




parham_m_s
journeyman

Jan 11, 2001, 9:24 AM

Post #2 of 4 (829 views)
Re: Arrays [In reply to] Can't Post

i'm not sure if this is what you're asking, but here goes:

if for example, u'r arrays are called @array1, and @array2, you can pull out certain values by doing this:

$array1[2] would be "c"
$array2[3] would be "i"
then just put em together to get "ci" using the . operator




waherne
Novice

Jan 11, 2001, 9:29 AM

Post #3 of 4 (828 views)
Re: Arrays [In reply to] Can't Post

Thanks for the reply.

I don't think though that the two arrays (@array1 & @array2)could be created in the first instance when the file was opened.

Any view?

Willie



randor
User

Jan 11, 2001, 10:24 AM

Post #4 of 4 (825 views)
Re: Arrays [In reply to] Can't Post

Willie,

ok:

"data.dat contains:

a|b|c|d|e
f|g|h|i|j

If data.dat is opened as an array, how does one identity the variables?"


from this i take it that that is exactly how the file looks, if so, then toget the vars from it you can do it this way:


open FILE, "file.dat" or die "cannot open file.txt";
@array = <FILE>; #this has placed the 2 lines in this array, each line is its own var.
close FILE;

# first we get rid of the return carriage and place all of them into 1 array
@firstbit = split(/\|/, @array[0]);
@secondbit = split(/\|/, @array[1]);
foreach $bit (@secondbit) { push(@firstbit, $bit);} #push all of second bits info into the @firstbit array
foreach $part (@firstbit) {print "$part\n";} # now we can print each one in the array out.

or to call to each one just use @firstbit[0], or @firstbit[3], ect....




 
 


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

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