LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2013, 06:31 PM   #1
captainentropy
Member
 
Registered: Mar 2010
Location: Berkeley
Distribution: Ubuntu, Mint, CentOS
Posts: 81

Rep: Reputation: 0
using Perl's range operation to print from one line to the end of file


If I have this file:

Code:
A
Header
With 
Unnecessary 
Information
5	9	32	9
1	67	57	97
10	32	8	28
and I want this:

Code:
5	9	32	9
1	67	57	97
10	32	8	28
How would I do that with Perl's range operator?

If I use this script

Code:
#!/usr/bin/perl -w

$data = $ARGV[0];

open (IN, "$data") or die "can't open data:$!";
 
@datafile = <IN>; #Read the data file and store it in an array

close IN;

print @datafile[4 .. 6];
I get

Code:
Information
5	9	32	9
1	67	57	97
But if I want to print every row after (and including) line 4 what would the the second variable in the range operator? I know I could just use an absurdly large number that would extend beyond the length of my file:
Code:
print @datafile[4 .. 10000000];
That works. Or this works:

Code:
$num = @datafile;
print @datafile[4 .. $num];
But what would the the easiest, i.e. something to use for X in @datafile[4 .. X]?
 
Old 08-24-2013, 02:16 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You can use $#array_name to get the index of the last element in an array.

Also be aware that, when using a range, the counter starts at 0 and not 1.

Have a look at this:
Code:
#!/usr/bin/perl

use strict ;
use warnings ;

my $infile = $ARGV[0] ;
my @datafile ;

open( IN, "$infile" ) or die "Can't open $infile : $!\n" ;
@datafile = <IN> ;
close $infile ;

print @datafile[ 5 .. $#datafile ] ;

exit 0 ;
Output would look like this:
Code:
5   9   32  9
1   67  57  97
10  32  8   28

Last edited by druuna; 08-24-2013 at 02:30 AM.
 
1 members found this post helpful.
Old 08-25-2013, 02:48 PM   #3
captainentropy
Member
 
Registered: Mar 2010
Location: Berkeley
Distribution: Ubuntu, Mint, CentOS
Posts: 81

Original Poster
Rep: Reputation: 0
Thanks druuna!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] PERL:How to print the lines of a opening file without '\n' in the end? jimtony Programming 2 11-21-2011 08:22 PM
Perl - How to stop range search at end of an array? DevonB Programming 1 12-07-2010 10:22 PM
[SOLVED] sed print range to end of file schneidz Programming 4 04-08-2009 01:30 PM
How to PRINT a range of letters of a string in Perl resetreset Programming 1 01-27-2009 08:42 AM
command or shell script to print line range from file minil Programming 3 12-28-2005 08:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration