Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old February 9th, 2001, 03:07 PM
FlipSide FlipSide is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Posts: 4 FlipSide User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to FlipSide
I know this is an easy question but i am having a mind block right now on how to do this...

If i open a file and store the contents into an array how can i get only the first 50 items in the array out and into another array ?

Thanks in advance,

Drew


Reply With Quote
  #2  
Old February 9th, 2001, 03:16 PM
mickalo's Avatar
mickalo mickalo is offline
Ole` Timer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Location: N.W. Iowa
Posts: 469 mickalo User rank is Private First Class (20 - 50 Reputation Level)mickalo User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 5 h 19 sec
Reputation Power: 8
Send a message via AIM to mickalo Send a message via MSN to mickalo
Here's one approach:
Code:
$i = 0;
@newdata = ();
open(FILE,"$some_file") || die $!;
@data = <FILE>;
close (FILE);
 LOOP: foreach (@data) {
  if ($i > 50) { last LOOP; }
    if ($i < 50) {
 push(@newdata,$_);
  }
  $i++;
 }


Now the @newdata array contains the first 50 lines from the @data arry.

Mickalo


[Edited by mickalo on 02-09-2001 at 02:28 PM]
__________________

Thunder Rain Internet Publishing

Custom Programming & Database development
Providing Personal/Business
Internet Solutions that work!

Reply With Quote
  #3  
Old February 10th, 2001, 07:35 AM
Adrian2 Adrian2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2000
Location: London, England
Posts: 251 Adrian2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Smile Array slices

As with all common tasks, there's a simple way to do it in Perl.

Code:
@a = qw{0 1 2 3 4 5 6 7 8 9 10};

@b = @a[0..4];

foreach (@b) {
	print;
}



@a has 11 elements, the numbers 0 to 10. We then assign a slice of elements 0 to 4 to @b. In your example, you'd want to slice from [0..49] to get the first 50 elements.

You can also select individual elements of an array like this.

eg. @b = @a[1,5,21]; # just elements 1, 5 and 21.

The .. operator isn't specifically associated with array slices, it just creates a list of values between the first and last, inclusive. In the code example above I could have loaded the @a array like this:

@a = (0..10);

but I wrote out all the values for clarity.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > An Easy one..(array)


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 | 
  
 





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