Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

perl coding

Original Message
Name: vande
Date: March 26, 2008 at 07:39:59 Pacific
Subject: perl coding
OS: Windows xp, Windows
CPU/Ram: 2.4 quad core Intel
Model/Manufacturer: Dell, Dell, Dell, Ho
Comment:
$lines = 0;
open(FILE, $filename) or die "Can't open `$filename': $!";
while (sysread FILE, $buffer, 4096) {
$lines += ($buffer =~ tr/\n//);
}
close FILE;

How is the above code work where it says file do i /home/vandelin/hello.pl?
or $filename gets replaced by the above? i tried both none seem to work i am just exploring and want to count the lines in hello.pl located under /home/vandelin

Learning in progress..........


Report Offensive Message For Removal


Response Number 1
Name: FishMonger
Date: March 26, 2008 at 08:15:27 Pacific
Subject: perl coding
Reply: (edit)
$filename is a variable that holds the name of the file that you want to count its lines. There are several ways to assign that var, either hard coding or dynamically set.


# hard code the assignment
$filename = '/home/vandelin/hello.pl';

$lines = 0;
open(FILE, $filename) or die "Can't open `$filename': $!";
while (sysread FILE, $buffer, 4096) {
$lines += ($buffer =~ tr/\n//);
}
close FILE;



Report Offensive Follow Up For Removal

Response Number 2
Name: ghostdog
Date: March 26, 2008 at 08:36:01 Pacific
Subject: perl coding
Reply: (edit)
you should probably start reading some Perl documentation.

Report Offensive Follow Up For Removal

Response Number 3
Name: vande
Date: March 26, 2008 at 09:38:19 Pacific
Subject: perl coding
Reply: (edit)
Well i was reading some doccumentation and i came accross this and was like hmm i will try this and i did try i tried a couple ways so its not that i came right here i tried really..... I just thought it would be cool to do i mean im going through some tutorials and doing this and that and just came to a standstill

Learning in progress..........


Report Offensive Follow Up For Removal

Response Number 4
Name: ghostdog
Date: March 26, 2008 at 18:22:16 Pacific
Subject: perl coding
Reply: (edit)
you should really start from the basics
Do a

perldoc perl

on the command land. See the various topics it has. Start from perlintro

perldoc perlintro

otherwise, you can also get a Perl book that teaches you the basics.

the typical algorithm to count lines is
1) open the file
2) using a loop, go through every line, each time incrementing a counter

Something like this (Python)


num=0
for line in open("file"):
#if its not blank lines, increment
if not line.strip() == "":
num+=1
print num


Report Offensive Follow Up For Removal

Response Number 5
Name: FishMonger
Date: March 26, 2008 at 19:39:32 Pacific
Subject: perl coding
Reply: (edit)
Even though counting the number of lines in a file is easy to do with either Perl or Python, neither of them is the best tool for that specific task.

The best tool would be the wc command.


[root@perlman ~]# cat test
#!/usr/bin/perl

use strict;
use warnings;
use YAPE::Regex::Explain;

my $regex = qr/([-\d.]+)\s*([-\d.]+)\s*(-[\d.]+)/;
my $exp = YAPE::Regex::Explain->new($regex)->explain;
print $exp;

[root@perlman ~]# wc -l < test
10

=================

Here's a good Perl beginners book:

Learning Perl, Fourth Edition
By Randal L. Schwartz, Tom Phoenix, brian d foy
http://www.oreilly.com/catalog/lear...


Report Offensive Follow Up For Removal


Response Number 6
Name: ghostdog
Date: March 26, 2008 at 21:09:55 Pacific
Subject: perl coding
Reply: (edit)
well, it really doesn't matter right?
Coding in Perl/Python, saving the script as wc.py or wc.pl, and use it like wc. Serves the same purpose.

Report Offensive Follow Up For Removal

Response Number 7
Name: FishMonger
Date: March 26, 2008 at 21:52:00 Pacific
Subject: perl coding
Reply: (edit)
Well, I guess you entirely missed my point.

My point had nothing to do with the naming of the script. My point related to reinventing the wheel, efficiency and using the right tool for the job.


Report Offensive Follow Up For Removal

Response Number 8
Name: ghostdog
Date: March 27, 2008 at 01:25:22 Pacific
Subject: perl coding
Reply: (edit)
why do you think using perl/python to solve trivial things like counting lines in a file is not considered as using the 'best tool for the job' ?

Report Offensive Follow Up For Removal

Response Number 9
Name: vande
Date: March 27, 2008 at 04:43:54 Pacific
Subject: perl coding
Reply: (edit)
Right cause if you ask me thats a pretty simple instruction. Why does it matter how you get it done ? or the tool you use to get it done? what are the benefits of using this tool compared to others ? is it easier?

Learning in progress..........


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: perl coding

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




XP Installed to G?

exessive internet traffic

ZoneAlarm Question. Blocked Connect

Windows Live Messenger Problem

Delete $Uninstall after SP3 updates


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC