Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

a little thing i need help with


  • Please log in to reply
7 replies to this topic

#1 shahar emule

shahar emule

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 03 August 2011 - 02:46 PM

hello all, what's up? i need your help here please
i don't common with perl, i programm other language as c sharp, but i have had to write a
simple algorithm to a sub-project, so i'd like someone help me to fix this little code:
open(my $fh, ">", "run_example.bash");
$line = <$fh>;
close($fh);


my ($i, $j);
for ($i=1; $i<(2/0.1); $i++)
{mkdir("run_check_$j", 0777) || print $!;
opendir(HU, "./run_check_$j") || print "Can't open... maybe try chmod 777";
@hu=readdir(HU);


$j = $i+1;
open(my $fh, ">", "run_example$j.bash") or die "Cann't open run_example$j.bash";;
print $fh "#One of many runs...: " , "\n", $line , "\n";
$celldun_now = 16-(0.1*$j);
$fh =~ s/celldm(1) =14/celldm(1) =$celldun_now;
close($fh);
closedir(HU);
}

explanation: i work in a directory there is a file i want to open, and to make many directories in this directory whice
are contain the same file but i want to change a code "$fh =~ s/celldm(1) =14/celldm(1) =$celldun_now;" in each new file accordingly. i also didn't know if i write the variable $i in a string, in a right way.

thank you a lot shahar

Edited by Roger, 04 August 2011 - 07:02 AM.
added [CODE][/CODE] tags

  • 0

#2 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1307 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 03 August 2011 - 03:19 PM

First of all, I don't know if this would work (it might, I don't know for sure):
open(my $fh, ">", "run_example.bash");
$line = <$fh>;
close($fh);
From what I know, it's supposed to be something more like this:
open FILE, "run_example.bash"; 
# You can use parentheses if you want to, but they're not required. 
$line= <FILE>; 
close FILE;
And you might want to declare the $line variable
my $line;
, sometime before this in the Perl file.

Then there's this part:
my ($i, $j);
for ($i=1; $i<(2/0.1); $i++)
{mkdir("run_check_$j", 0777) || print $!;
opendir(HU, "./run_check_$j") || print "Can't open... maybe try chmod 777";
@hu=readdir(HU);
The "||"'s are supposed to be either "|"'s or "or"'s.
Then the other thing about that, what is $j set to when you call the mkdir and opendir functions?

Then this:
$j = $i+1;
open(my $fh, ">", "run_example$j.bash") or die "Cann't open run_example$j.bash";;
print $fh "#One of many runs...: " , "\n", $line , "\n";
$celldun_now = 16-(0.1*$j);
$fh =~ s/celldm(1) =14/celldm(1) =$celldun_now;
close($fh);
closedir(HU);
}
You open a file and use the handle more like this:
open FH, ">", "run_example$j.bash" or die "Can't open file run_example$j.bash"; 
print FH "#One of many runs...: " , "\n", $line , "\n";
But I didn't understand this part:
$fh =~ s/celldm(1) =14/celldm(1) =$celldun_now;
Isn't =~ a string operator, not a file handle operator?

But as for your last question, yes, to insert $i into a string you can use "some_text...$i...some_more_text..."; or you can use "some_text..." . $i . "...some_more_text...", it's up to you what to use.




EDIT:

open(my $fh, ">", "run_example.bash");
$line = <$fh>;
close($fh);

If you're trying to read the file, you don't need to use ">". ">" is for writing and ">>" is for appending. You just skip that parameter if you want to open the file for reading.
  • 0

#3 shahar emule

shahar emule

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 03 August 2011 - 11:36 PM

you helped me a lot thank you, by the way the line that is no understood:

$fh =~ s/celldm(1) =14/celldm(1) =$celldun_now;
i wanted to change some lines in the new files i create so if it was "celldm(1) =14", then i want it to be
"celldm(1) =$celldun_now" where $celldun_now is a variable that i define before (its just a subprogramm in physics project) in addition can you explain me the difference between my $x to $x (what is the word my refers to)?

i will try to run the progamm in some hours.

meanwile you help me;):rolleyes:



  • 0

#4 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1307 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 04 August 2011 - 12:34 AM

'my' is used for declaring variables.

You can either make Perl programs with 'use strict;' or without 'use strict;'. If you make the program with using strict, then the 'my' is required; it's optional otherwise. But there is an advantage to using strict.

A program that uses strict:
#!/usr/bin/perl 

use strict; 
use warnings; 

# Those two lines are nice to have in programs. 
# The 'use strict;' line is the line we're talking about. 

my $variable; 

$variable= "some_text"; 

print $variable; 

my $text= "some_text"; 

print $text; 

print " ... $text";

So let's say we don't use strict, and we miss-spell a variable name:
#!/usr/bin/perl 

$username= "some_user"; 

$a= $usrename; 

# ... do some more stuff with $a ...  

print $a;
In that case perl (notice lower-case 'p' when we're referring to the program that runs Perl programs) would make a new variable named $usrename and set it to blank and then use that. So then it can be pretty hard to catch the spelling error, especially if you have a lot of code.

But if you use strict, perl would catch the mistake and tell you where it found the mistake. It'll be in an error form, but then at least you can fix it, so that error doesn't happen again.

And also, you can only declare a variable once. That's why I usually prefer to declare variables first, and then use them, in case I would need that variable again somewhere else in the code.

That's about as much as I know about 'my'.
  • 0

#5 shahar emule

shahar emule

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 04 August 2011 - 01:06 AM

great, now i understand :) and did you know if i write the change of text in file in right way? :
$fh =~ s/celldm(1) =14/celldm(1) =$celldun_now;
i wanted to change some lines in the new files i create so if it was "celldm(1) =14", then i want it to be
"celldm(1) =$celldun_now" where $celldun_now is a variable that i define before (its just a subprogramm in physics project)


thank you again,
shahar
  • 0

#6 shahar emule

shahar emule

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 04 August 2011 - 05:48 AM

oo and one more thing what are the line : @hu=readdir(HU);
that you had add is neccessary for?
did i had to write close(FH); now after i change as you say?
and on the question on my previous messege on changing string : $fh =~ s/celldm(1) =14/celldm(1) =$celldun_now;
did i wrote it right?

thank you a lot:rolleyes:
  • 0

#7 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1307 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 04 August 2011 - 09:16 AM

Do you need to write close(FH)? I think you do; well, perl would probably close the file for you if you forget to, but it's nice to clean up after doing work.

As for the =~ operator, I don't know if it works for file handles. But to search for and replace one match of $what with $with, it's something like this:
my $text= "hello username, how are you? "; 
my $what= "username"; 
my $with= "the_actual_name"; 
$text =~ s/$what/$with/;
To replace all the occurrences of $what with $with, not just the first one:
$text =~ s/$what/$with/g;
Of course, you can also use immediate strings, rather than variables:
$text ~= s/somestring/someotherstring/g;

Here's a program I wrote that replaces every digit "1" with the word "one" in the file "file01.txt" (but you can do something similar, I think):
#!/strawberry/perl/bin/perl.exe 

use strict; 
use warnings; 

# Tell the server that we're done with the HTTP header, 
# and that we are now sending the data (HTML): 
print "\r\n\r\n"; 

# Declare some variables. 
my $filesize; 
my $text; 

# CRLF is used for making a new line ("\r\n"); 
# though some places use just LF ("\n"). 
my $cr= "\r"; 
my $lf= "\n"; 

# Open the file for reading. 
open FILE, "file01.txt"; 
# In case the file has sensitive data in it, we 
# want to be careful with how we handle the file. 
# binmode means we're trying to handle things in 
# bytes, and not in lines, as far as I know. 
binmode FILE; 
# Get the file contents (not recommended for very large files, in what case you would work with little bits of the file at a time). 
$filesize= -s "file01.txt"; 
read FILE, $text, $filesize; 
# Then we close the file. 
close FILE; 

# Do the job. 
$text =~ s/1/one/g; 

# Open the file for writing. 
open FILE, ">file01.txt"; 
binmode FILE; 
# Write the text to the file. 
print FILE $text; 
# And close the file. 
close FILE;

Edited by RhetoricalRuvim, 04 August 2011 - 10:23 AM.

  • 0

#8 shahar emule

shahar emule

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 04 August 2011 - 11:56 AM

ok i have tried to run my script but theres one problem, i have an array that i have compared to
<$fh> like this:
my @line;
@line = <$fh>;

(i use array because i wanted to read all the lines in the file i opened)
so now the array contain strings
but when i want to change part in those strings (a little line) i can't because its an array it makes an error.
i prefere to do it in this way, there is something you maby know i can try?

thank you a lot - almost succeede;)d
  • 0