Follow us on Twitter!
Society leans ever heavily on computers, if you have the power to take out computers you can take out society. - cubeman372
Thursday, April 24, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 22
Guests Online: 19
Members Online: 3

Registered Members: 82886
Newest Member: The Slummy
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Perl Problem.

xxSk1N_D33Pxx
Member

Your avatar

Posts: 32
Location: London, England
Joined: 01.05.07
Rank:
Newbie
Posted on 14-10-08 01:00
I've just started learning this language and have come up against a problem I can't seem to get my head around.

At the moment i have a script that reads content from a file and prints it to the console, however my aim is to append the string '#' to the beginning of each newline, without using a loop.

The following is the code for the script:

Code

#!/usr/bin/perl

$file = '/home/sk1n/test.txt';
open(INFO, $file);
@line = <INFO>;
close(INFO);
print @line;






I'll highly appreciate any input.


i17.tinypic.com/53oukih.jpg
Japanesedude: Java turns me on
Author

RE: Perl Problem.


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 14-10-08 01:07
You could try doing a string replace of \r for \r# (or similar, depending on which line break works). Other than that, I don't think there's any way for you to give special attention to each line of a file unless you do a loop. Any particular reason you're against using a loop?


Author

RE: Perl Problem.

spyware
Member



Posts: 4192
Location:
Joined: 14.04.07
Rank:
God
Warn Level: 90
Posted on 14-10-08 01:07
Use the foreach loop.



img507.imageshack.us/img507/3580/spynewsig3il1.png
"The chowner of property." - Zeph
[small]
Widespread intellectual and moral docility may be convenient for leaders in the short term,
but it is suicidal for nations in the long term.
- Carl Sagan
[center]�Since the grid is inescapable, what were the earlier lasers about? Does the corridor have a sense of humor?� - Ebert[/ce
Author

RE: Perl Problem.

xxSk1N_D33Pxx
Member

Your avatar

Posts: 32
Location: London, England
Joined: 01.05.07
Rank:
Newbie
Posted on 14-10-08 01:23
Zephyr_Pure wrote:
Any particular reason you're against using a loop?


The guide I'm following has not introduced the loop in Perl, yet.

One of the exercises is to append the string '#', although it does not explicitly state that a loop can not be used. I just assumed it couldn't, based on it not introducing them at this point.

However I will look into the method you suggested.

Thank you both for your input, much appreciated.

Edit:

Solved the problem with the following:

Code

<snip>
s/\A/#/ for @line;
</snip>





Thank you Zepyhr_Pure.



i17.tinypic.com/53oukih.jpg
Japanesedude: Java turns me on


Edited by xxSk1N_D33Pxx on 14-10-08 02:05
Author

RE: Perl Problem.

clone4
Member



Posts: 586
Location: He is back and he's bad!
Joined: 25.11.07
Rank:
Mad User
Posted on 14-10-08 09:07
which is short version for:

for (@line) {
$_ =~ s/\A/#/;
}

So you still use loop, as everybody said... And I would advice when you are begginer to avoid the shortening of code to one liners as much as possible, firstly it's less readable for your own future reference and at the beginning you should learn how to code things properly... Just my 2 cents


[img][/img]img164.imageshack.us/img164/5713/perlvl0.jpg

clone4.freehostia.com/ubuntu_3.png
spyware - "They see me trollin'..."
<yaragn> ever seen that movie? The Matrix?
<yaragn> with those green lines of flying text?
<yaragn> *THAT'S* Perl

clone_4@hotmail.com
Author

RE: Perl Problem.

xxSk1N_D33Pxx
Member

Your avatar

Posts: 32
Location: London, England
Joined: 01.05.07
Rank:
Newbie
Posted on 14-10-08 19:58
Thank you Clone, I never actually knew that was just a shorter version.



i17.tinypic.com/53oukih.jpg
Japanesedude: Java turns me on
Author

RE: Perl Problem.

clone4
Member



Posts: 586
Location: He is back and he's bad!
Joined: 25.11.07
Rank:
Mad User
Posted on 14-10-08 23:09
xxSk1N_D33Pxx wrote:
Thank you Clone, I never actually knew that was just a shorter version.


No problem Wink and if you are stuck somewhere don't worry to pm me, or write me on msn. Helping somebody is always good revision Smile


[img][/img]img164.imageshack.us/img164/5713/perlvl0.jpg

clone4.freehostia.com/ubuntu_3.png
spyware - "They see me trollin'..."
<yaragn> ever seen that movie? The Matrix?
<yaragn> with those green lines of flying text?
<yaragn> *THAT'S* Perl

clone_4@hotmail.com