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:
Post deleted by sdlan75

 



sdlan75
New User

May 25, 2013, 4:09 AM

Post #1 of 5 (674 views)
Post deleted by sdlan75

 


Laurent_R
Veteran / Moderator

May 25, 2013, 4:32 AM

Post #2 of 5 (671 views)
Re: [sdlan75] How to read only the first line [In reply to] Can't Post

This is not clear to me, it is a bit contradicory.

If you want to print only the first line of your file (as per the title of your post), you will print:


Code
FullName


If this is what you want, you can do something like this (I have modified your code to reflect generally admitted best practices) :


Code
#!/usr/bin/perl 
use strict;
use warnings;

my ($option, $myfile) = @ARGV;

open my $INFILE, "<", $myfile or die "cannot open $myfile $!\n";
my $line = <$INFILE>;
if ($option eq "n") {
print $line;
}
close $INFILE;


But that is presumably not what you want.

Do you want to print the first employee (second line)? or the first field of each line? or something else? Please explain.


(This post was edited by Laurent_R on May 25, 2013, 4:55 AM)


sdlan75
New User

May 25, 2013, 5:10 AM

Post #3 of 5 (663 views)
Post deleted by sdlan75 [In reply to]

 


Laurent_R
Veteran / Moderator

May 25, 2013, 6:30 AM

Post #4 of 5 (610 views)
Re: [sdlan75] How to read only the first line [In reply to] Can't Post

Hi,

try this:


Code
#!/usr/bin/perl 

use strict;
use warnings;


my $option = $ARGV[0];
while (<DATA>) {
next if /^Full-Name Salary/;
if ($option eq "n") {
#Get rid of the new Line Character
chomp;
my @fields = split (/ /, $_);
print "@fields[0,1] \n";
}
}
__DATA__
Full-Name Salary Bounce
Jack Smith 60,000 1000
Tom Lee 90,000 1500
Stephen Ho 120,000 2000


Running the script:


Code
$ perl  first.pl n 
Jack Smith
Tom Lee
Stephen Ho



FishMonger
Veteran / Moderator

May 25, 2013, 8:44 AM

Post #5 of 5 (603 views)
Re: [sdlan75] How to read only the first line [In reply to] Can't Post

Here's a skeleton of the script I'd work with.


Code
#!/usr/bin/perl 

use 5.10.1;
use strict;
use warnings;

@ARGV == 2 or die "wrong number of args\n";
my ($option, $file) = @ARGV;

my %actions = (
'-n' => \&print_name,
'-s' => \&print_salary,
'-b' => \&print_bonus,
usage => \&print_usage,
);

my $action = $actions{$option} || $actions{usage};
$action->($file);


sub parse_file {
# code that parses the file and returns the data in a hash
# hash keys would be 'name', 'salary', and 'bonus'.
}

sub print_name {
my %employees = parse_file($_[0]);

}

sub print_salary {
my %employees = parse_file($_[0]);

}

sub print_bonus {
my %employees = parse_file($_[0]);

}

sub print_usage {
print "Invalid arg was passed\n",
"Usage: $0 [-n -b -s] filename\n";
}


The parsing of the file could be done outside of the "print" subs to eliminate the duplication of that statement in each sub.


(This post was edited by FishMonger on May 25, 2013, 8:45 AM)

 
 


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

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