Perl Script that counts lines of Lyrics

Write a Perl script which displays the NUMBER of lines in a text tile, then appends “My script is complete” to the file. This will require meto open the file twice, once to read everything in and once to append. Append is >> onemoretime.txt. Read is > onemoretime.txt. The Britney Spears lyrics is in OneMoretime.txt that needs to be read and appended.

Onemoretime.txt contents

Oh baby, baby
How was I supposed to know
That somethin’ wasn’t right?

Oh baby, baby
I shouldn’t have let you go
And now you’re outta sight

My loneliness is killin’ me
I must confess I still believe
When I’m not with you I lose my mind
Give me a sign, hit me baby one more time

How would type up such a Perl Script?

#!/usr/bin/perl -w

use CGI qw(:all);;
use Fcntl qw(:flock);

my $mldata=“onemoretime.txt”;
my $semaphore_file=“onemoretime.txt”;

$file=‘SnPmaster.txt’;
open(INFO, $file) or die(“Could not open file.”);

$count = 0;
foreach $line (<INFO>) {
print $line;
if ($++counter == 2){
last;
}
}
close(INFO);

sub get_lock {
open(SEM, “$onemoretime.txt”) || die “Cannot open $onemoretime.txt : $!”;
flock SEM, LOCK_EX;
}

sub release_lock {
close(SEM);
}

get_lock();
open(GB, “>>$mldata”) || die “Cannot open $mldata: $!”;
print GB "address: ", param(’_ADDRESSVARIABLE’), "
";
close(GB);
release_lock();
}

On Thu, 16 Aug 2012 20:36:02 +0000, renegadeviking wrote:

> How would type up such a Perl Script?

I’ll have to admit that this sounds an awful lot like a homework
assignment. As such, it wouldn’t be prudent to just give you the answer,
but perhaps you’re working through an issue with a script you’ve started
on?

Jim


Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

I need this to work, thanks. If I am working on openSUSE at work, is it homework. Is it my username?

On Thu, 16 Aug 2012 22:16:02 +0000, renegadeviking wrote:

> I need this to work, thanks. If I am working on openSUSE at work, is it
> homework. Is it my username?

What’s not working with it?

No, it’s not your username, it’s just the nature of what the script is
doing. It reads like a homework question - and we do get our share of
students coming into the programming group looking for “help” in the form
of “I can’t figure out this assignment and want someone to do it for
me” (though it’s never explicitly stated that way).

So we tend to be careful about providing direct answers to questions that
appear to be school assignments.

Jim


Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

On Thu, 16 Aug 2012 22:42:58 +0000, Jim Henderson wrote:

> What’s not working with it?

(We also ask that code be placed in code tags - see the advanced editor,
“#” button)

Jim


Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

We also ask that you stick to one thread per topic. I responded to your
other one before seeing this one, but they appear to be the same
homework question.

Another reason this is a homework question: Nobody would write a Perl
script to do what ‘grep’ can do in one line unless it was as a tiny
practice assignment.

Good luck.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.18 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJQLYaTAAoJEF+XTK08PnB5jSgQAKJMlA7+qVynt0jhSwvF7M/o
nG+DQPD8yD5ZDogpsShr3oPvJEKTHvmOR9384bfMplroPdL1UHmwiRQf0Efd6d07
jJc9pdPQoHRXoFw3iMC8Z8X3+2HKglpUlrILW4TcM8h3H9fs0C7ZykOr0f5zamdZ
7huHhqb4BAQxPRvBD2qwDitkXwlbk2oMHL/o09nkPhuuN9HwVdkcHj6ttKH1QvU9
4DmEazTZ2yz9fYONrb30keIcQtViDXA57DVPcF4JrODjtpRPvzod37/+uUGGtOTL
BsEwhrq1aYrdmaqPMc0s7Km2wFO5yCY5uPxyIuPaxmP6G7pZ9gs1jwF/Is+nDrPo
VNulKSwzm7bGP5cdMRELAphh11P9Qowfovn+PA0yidRFj3974tT25dTF+PbnzFDW
CSDvddPn7e6D8hHyTaUvI4VkTakAGqMi1GsDpmfC2sc4OThlMHYyUeo9Un3O7ipG
JTCWHKXCVokUK6Wpiw7nGk3TTjkZxz22amIksiaudf/KolqBTNnAP5RfY/1ZVE3o
+9gagFMl6UAoJwI//2XFUlEXhvzW5o52dCi1Ig9QTBzyGZJJ2Ym4fjWOO6xobD/i
G1Qlbr/vX4+7HUe8Dwwfp2ck3Wfe6pRZV101GNmnWwBdanWzVrVAGq2mKwm9OxS9
91aK3ZSWMmlNqg18kXQV
=iJnj
-----END PGP SIGNATURE-----

On 08/17/2012 01:47 AM, ab wrote:
> Nobody would write a Perl
> script to do what ‘grep’ can do in one line unless it was as a tiny
> practice assignment.

+1


dd

Am 17.08.2012 01:47, schrieb ab:
> Another reason this is a homework question: Nobody would write a Perl
> script to do what ‘grep’ can do in one line unless it was as a tiny
> practice assignment.
I disagree, you would do that and not rarely but often (cannot speak
about Perl, but that’s programming language agnostic) if the same
program needs to run on different platforms or if it is part of a larger
program and a call to an external tool is in general a problem
(security, no defined API to interface with …).
Sorry, could not resist. :slight_smile:

On the other hand this problem is so funny, it can only be a homework or
textbook problem.


PC: oS 12.2 x86_64 | i7-2600@3.40GHz | 16GB | KDE 4.8.4 | GeForce GT 420
ThinkPad E320: oS 12.1 x86_64 | i3@2.30GHz | 8GB | KDE 4.8.5 | HD 3000
eCAFE 800: oS 12.1 i586 | AMD Geode LX 800@500MHz | 512MB | KDE 3.5.10

In fact Perl has a grep command.


#!/bin/bash
wc -l Onemoretime.txt
echo "My script is  complete" >>Onemoretime.txt

Or do I misunderstand the need for grep?

Martin Helm wrote:
> In fact Perl has a grep command.

True, but I sometimes still call system grep from within my perl
programs. It all depends on what you’re searching for and how big the
file(s) you’re searching are.

Am 17.08.2012 12:26, schrieb hcvv:
> Or do I misunderstand the need for grep?

From what I (mis)understood it was about numbering the lines not
counting them, so I thought one of


cat -n
grep -n '^'

should do it.

But rereading the original question, it seems you are right it is just
about counting lines not numbering them.


PC: oS 12.2 x86_64 | i7-2600@3.40GHz | 16GB | KDE 4.8.4 | GeForce GT 420
ThinkPad E320: oS 12.1 x86_64 | i3@2.30GHz | 8GB | KDE 4.8.5 | HD 3000
eCAFE 800: oS 12.1 i586 | AMD Geode LX 800@500MHz | 512MB | KDE 3.5.10

Well, the OP is as bad in making his specifications as he is in doing his system analysis as he is in programming.
Thus we could all be wrong. rotfl!

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The reason for 'grep and not ‘wc’ is because you need lines that are not
blank. wc will return them all, and grep -cv '^\s*$' will return just
the number you’re after instead.

Good luck.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.18 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJQLmQ1AAoJEF+XTK08PnB5cf4P/irM48Eba6e9jOKGGaWhwuZm
+qfeFaf6Gk9hEm2PXUe1aOBsE6I7t60FBXGPYoC6in1qUBdpWlF5RmgmMiWig6Pu
fB8+SnImlrgZdXMkUp5KHYO87JyQIRNFm5UlhhBd+/LpMrWYOgXEYtc75R5xX9OD
d+Lt9fmmVWENqqymw1STytjccF7p0DwaQ0U+DI2HpMMmZ5zQerFmUdoXUEX2QpYR
r9MqaiUgFaXvYxntkWNgdgtrgw8yftWcdU2YNBqGR4f/6TVmjeV7TM1drcgSjd0u
BofETdv9yHiu8y56Lugz9Jh8SsYCA43M1MJj5BYKpcN7/OJt1L5SdgJ6OEj2MEl2
8W9Yq69z3XNgjCxB+OZ6niZEaZia+Mt6yR7D8V4Gj47X2haLwGT9FeW5RvxtI/No
oIM/3MsFdfPna7+yLG832i27Sc7ooa8d7N7aJEe+I9m0OZJWaZYhHhpwCosgG7KJ
orAF3MgVNZPSjPSz/Utlcde/UIT9yEntwGU/avi+h6TZHLAq8N2iOn3lJ4FP26Zc
iVZbrcr4yoCcSiZTCR27gt9oNXVLblHlgzvSX8On5bpSB/jmndzE+YcQV1Y51Rki
dKnLr9b56j1j1Dm5lUkZCbD4aK4i32vUsVqjkyH1f+6s0Vk4xLurPCdpTJOVS0Dv
jJ3Xwm13RmMUSiptUWRm
=Puw5
-----END PGP SIGNATURE-----

Am 17.08.2012 17:33, schrieb ab:
> The reason for 'grep and not ‘wc’ is because you need lines that are not
> blank.
Now I seem to be blind, where do you see that requirement?


PC: oS 12.2 x86_64 | i7-2600@3.40GHz | 16GB | KDE 4.8.4 | GeForce GT 420
ThinkPad E320: oS 12.1 x86_64 | i3@2.30GHz | 8GB | KDE 4.8.5 | HD 3000
eCAFE 800: oS 12.1 i586 | AMD Geode LX 800@500MHz | 512MB | KDE 3.5.10

Same here. I reread the first post above three times, but I do not see it.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Title of thread: “Perl Script that counts lines of Lyrics”

Blank lines lack lyrics.

Good luck.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.18 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJQLnE2AAoJEF+XTK08PnB5tfMP/RqpedfuTdci8kbZgQTLHH3W
i8M/Y0sVYg6K1zLqPh0kj6MnwdUn0b+xPKhd4aXLv7ZnkBH2o3Qq4HDF8tuB1kUo
r4oKwts4fZ3Z0KcKQGdTR27Pt00Z/JP3B7YIKEevjiiYvgNeuDA+hB/pFQvqbcJ+
risjMu5UUTex8VOFR+Og2oDMnZ3nOZdahokJ75eyZLCynkcdk4Ih/IaMOY/TixKU
6SkBrCpFUZtngAqbXZ+c9kpWzWxQHPD64r781J4TbpmbwzqmNUb5aLmCESu6moia
nCTid//GybbzXaEatsXBJaF1br4YRWR9hMvR+gaW1aq850ZZ4UFA9yEEk/ny1bLV
hONFtUZw2qxrdBdqHVXCJ9LpLK2SkypdqDRasBDbFKTSAelLmf88N9X6Xu0IIiy6
IqF4tUKyvj5/BcIaC0htETi2VxxR0/lBcbL51e8+i+dn0eSeO04oJ5xtRa3YhI+4
k6Jxl26IEDZrc5VdnsVM/pDi/kytSOrGLZ1i72hydnaarajdzoCZTg54eBbD2DlZ
jHXz5t2ro4CcLOzTfCwoOq64k91HMu7nmWqasl6wO7vr3WwYbpVpuvDilfri8pw/
GJstE5B+LfyW1ADTui2iKh/LrxZ5o/5J1XCPJBig05M13446oLQZoVokDqcQQEFC
Bear3fcM/OwqTT+OkzE/
=p64b
-----END PGP SIGNATURE-----

First I do not agree. Lay-out is part of the lyrics IMHO. Bit I admit that I can be wrong. After all this is not a forums about singsongs.

It is a computer technical forum and the OP specified his requirements clearly in computer technical terms as
[QUOTEwhich displays the NUMBER of lines in a text tile][/QUOTE]
I like to point to the word “file” that has a well know meaning in a forum like this.

Depends if the singer is ‘blonde’ or not :wink:

I didn’t count them…


<some perl code> if !/^\s+?$/; }


Cheers Malcolm °¿° (Linux Counter #276890)
SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 3.0.34-0.7-default
up 19 days 13:21, 2 users, load average: 0.42, 0.30, 0.37
CPU Intel i5 CPU M520@2.40GHz | Intel Arrandale GPU