Beginner Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherBeginner 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:
Learn four approaches for automating Excel logic, along with advantages and disadvantages of each. Read all about it in the free whitepaper: “Tapping into Excel Logic from Java: Four Server-Based AlternativesDownload Now!
  #1  
Old September 30th, 2002, 02:19 PM
NinxX NinxX is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 4 NinxX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Grappling with arrays...

I have a mental block trying to work with arrays and I was hoping someone could explain/show me how it should be done.

I wrote a script (just to train myself) which created a flat file tab delimited db and felt rather smug. However the next part has caused me really to wrestle with connecting identities to the contents of an array.

I'll show you what I did, (I got the job done, at least!) and then pose some questions:

Code:
open (DATA, "$text1dat") || die "Couldn't get data page";
@all=<DATA>;
close (DATA);

@allnew = split (/\t/, $all[0]);

$sitename = "$allnew[0]";
$pagename = "$allnew[1]";
$text1 = "$allnew[2]";
$text2 = "$allnew[3]";
$text3 = "$allnew[4]";
$text4 = "$allnew[5]";


It was a real struggle to get to that (and I'm pleased because I now understand the fundamentals a lot better), and it did the job but it doesn't seem to be very 'perl' - just basic and cumbersome.

How can I express it so that the scalars match up with the correct elements of the array in more succinct fashion (with explanation, please! )

Also, is it possible to make the lists open ended so that the number of elements and scalars expands to the requirements of the moment? Eg in one use there might be two elements, another a dozen, which would need to be tagged with a scalar. If so how might it be done?

Thanks very much

Reply With Quote
  #2  
Old September 30th, 2002, 04:54 PM
Ctb's Avatar
Ctb Ctb is offline
An Ominous Coward
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2002
Posts: 4,425 Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 3 Weeks 10 h
Reputation Power: 0
TMTOWTDI (tim-t-ow-dee) - There's More Than One Way To Do It
Code:
open (DATA, "$text1dat") || die "Couldn't get data page";
@all=<DATA>;
close (DATA);


while($line = shift @all) {
	($sitename, $pagename, $text1, $text2, $text3, $text4) = split(/\t/, $line);
}

Code:
open (DATA, "$text1dat") || die "Couldn't get data page";
@all=<DATA>;
close (DATA);


foreach $line(@all) {
	($sitename, $pagename, $text1, $text2, $text3, $text4) = split(/\t/, $line);
}

There's lots of ways to do it! In the first example, I use the shift function to remove the first element of the array ([0]) and return it to $line. That shortens the array by 1 (that is, [0] goes into $line and [1] becomes the new [0], [2] becomes the new [1], etc.). If you know, the number of elements that are going to be coming out of the line, you can assign them directly to scalars. The split operation works left to right on $line, so whatever the first "field" of $line is goes into $sitename, the second field goes to $pagename, etc. Then, I go back up to the while loop again and shift @all another time, thus repeating the process. When @ all has a 0 length, shift returns a false value (I believe, it actually returns the undef value) and the loop ends. This method, of course, does NOT preserve @all as it will eventually get emptied.

The second method, however, does preserve @all. foreach $line(@all) does exactly what it sounds like:
foreach $line in @all it does the stuff in the block (curly braces). The stuff in the block is exactly the same as above. The only difference here is that when foreach() is done the array remains unchanged so you can use it again if you want.

Other array functions you may want to read about in the perlfunc manpage (or, HTML page if you use Win32 Activestate):

splice, pop, push, unshift - all relatives of shift in some way
sort, reverse, join
More advanced list stuff: grep, map, unpack, pack

Incidentally:
"$allnew[x]" <- the quotes are not "magical", that is, they don't do anything. You don't need them in this context.

Have fun

Reply With Quote
  #3  
Old October 1st, 2002, 02:27 AM
NinxX NinxX is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 4 NinxX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Wow - there's enough there for a month! Thanks a lot, Ctb.

I had looked at the 'for each' option but was getting it totally wrong.
Instead of creating a scalar ($line), using it then dumping it (you callous brute! ) I worried about defining it and what would happen to it afterwards.

I feel like a weight has been lifted...

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherBeginner Programming > Grappling with arrays...


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway