I am having this error in the line self->{_seedpath} below..can somebody help!!!!
my @seeds = glob("*r0.32.obj");
@seeds = sort @seeds;
my $seed = pop @seeds;
$self->{_seedPath} = "$self->{_work_dir}/$seed";
| SitePoint Sponsor |
I am having this error in the line self->{_seedpath} below..can somebody help!!!!
my @seeds = glob("*r0.32.obj");
@seeds = sort @seeds;
my $seed = pop @seeds;
$self->{_seedPath} = "$self->{_work_dir}/$seed";





my $seed = pop @seeds;
You should check to see if something's actually getting popped out before going on to the next line?
I'd write it as:
Code Perl:
That way the expressions in the if() conditions aren't reached unless $seed is true (defined, etc)
Also the output from glob() can directly be fed into sort(), so your @seeds will be what you want in one pretty line of code.
Thanks!!!yeah..the seed isnt valid anyway
![]()
I have trouble to understand these matching equation and foreach function trying to doanyone can try to explain to me? thanks a million!!
my $testtest="test.txt";
open (INFILE,"$testtest");
my @line=<INFILE>;
close(INFILE);
foreach (@lines) {
my $line = $_;
# Search for new header
if ($line =~ /^\[(.*)\]/) {
if ($new) {
$header = $1; next;
} else {
$new = 1; $header = $1; next;
}
}
Code Perl:my $testtest="test.txt"; open (INFILE,$testtest) or die("Cannot open $testtest: $!"); # It's good to check for I/O failure my @line=<INFILE>; # read all lines into @line array close(INFILE); foreach (@lines) { # iterate over every line in @lines my $line = $_; # set $line to current line, which is $_ if ($line =~ /^\[(.*)\]/) { # see if a pattern matches if ($new) { # $new = true? $header = $1; # set $header to what you've captured in the pattern match (.*) next; # next line } else { # $new = false? $new = 1; # set $new to true $header = $1; # set $header to what you've captured in the pattern match (.*) next; # next line } } }
If you use foreach, you can also do
That way you don't have to assign $line to $_ for every iteration.Code:foreach my $line (@lines) { ... }
Instead of open() you can also use File::Slurp, see http://search.cpan.org/dist/File-Slu.../File/Slurp.pm
Thanks for the clear explanation but from the code I see, the $new is initialize as 0
so that is mean it wont go into if loop in the first line if the pattern matched, right?
But what is the purpose of doing that?
I am rather confused of the below highlighter item as well...what does it trying to do?=.=
# Read Header
if ($header =~ /PI/) { #
if ($line =~ /psmi_release\s+(\S+)/) { $pitool = $1;}
if ($line =~ /handler\s+(\S+)/) { $handler = $1;}
if ($line =~ /model\s+(\S+)/) {$mdl = $1;}
next;
} else {
if ( $line =~ /^(\S+)\S+)\s+(\S+)\s+(.*)/ ) {
$options->{$header}->{$1}->{$2} = $3;
}
}
Please wrap your code in tags, it is more readable that way.
means "address the item in hashreference $options with the hashkey equal to the value of $header. So if $header equals 'foo', it'd be $header->{foo}.Code:$options->{$header}
$1, $2 and $3 are regular expression captures. The first, second and third capture. So
which can also be more cleanly written asCode:$options->{$header}->{$1}->{$2} = $3meansCode:$options->{$header}{$1}{$2} = $3
I suggest you read up on basic Perl data structure and reference use; see http://perldoc.perl.org/perlref.html and http://perldoc.perl.org/perldsc.htmlCode:Set the hashvalue corresponding with hashkey $2 [which is the hashvalue corresponding with hashkey $1 [which is the hashvalue corresponding with hashkey $header [which is a hashkey of hashreference $options] ] ] to $3
Thanks for the pointer. I have googled and actually read through the link pasted but I couldn't get the meaning when it does the double referencing {}->{}->
FYI, this is not my code and i start picking up perl so it is really hard for me to visualize what it is trying to do.
The test even have something like %$tests variable but i couldnt found it in google
for my $test (sort keys %$tests )
{
my ($seed, $seed2, $seed3, $seed4, $seed5, $nt, $pfmn);
for my $label (keys %{$tests->{ $test }} ) {
The documentation I gave you in my previous post explains reference use.
$ => scalar
@ => array
% => hash
%$hash_reference : dereference the hashreference contained in the scalar $hash_reference
@$array_reference : dereference the arrayreference contained in the scalar $array_reference
etcetera.
I would really suggest you read up on Perl. Try to get your hands on O'reilly's Programming Perl book, or O'reilly's Learning Perl. Also, http://www.perlmonks.org has loads of documentation and tutorials. So does http://perldoc.perl.org.
Good luck!
Alright. thanks for the great help!!! appreciate it!
Glad it's useful.
If you want to be explicit in your dereferencing, you can also do:
@{ $array_reference }[0]
%{ $hash_reference }{foo}
compare this to
$array_reference->[0]
$hash_reference->{foo}
Cheers.
Appreciate if anyone can help! I am confused on how the file is calling when learning up the perl script.
FYI, I have files called rr.pl and cafe.pm
rr.pl file
it does "use cafe" that i am not sure if this is the way to call to cafe.pm file
cafe.pm fileCode:use cafe
inside it will do the package cafe
How does the code below (in rr.pl file) works with the cafe.pm file as I saw there is a sub new() function calling in cafe.pm. And I am still confused on how the referencing works.Code:package cafe
And I try to print out the array cafes but it comes with some hash number. What is the right way to print out these variable if i want to see what it pass into these?Code:my $temp = cafe->new(%cafe_switches);
Code:push(@cafes, $temp);
Hi.
.pl = perl script.
.pm = perl module.
"use modulename" (so "use cafe" in case of "cafe.pm") is the correct way of including that.
It seems the cafe package defines a class somewhere, which is instantiated with the new() subroutine. For more information on this, please read up on Perl Object Oriented programming, it's probably a bit different to what you're used to.cafe.pm fileCode:use cafe
inside it will do the package cafe
How does the code below (in rr.pl file) works with the cafe.pm file as I saw there is a sub new() function calling in cafe.pm. And I am still confused on how the referencing works.Code:package cafe
Code:my $temp = cafe->new(%cafe_switches);
Please use Data::Dumper, you can install it off CPAN.And I try to print out the array cafes but it comes with some hash number. What is the right way to print out these variable if i want to see what it pass into these?:(
Code:push(@cafes, $temp);
Thank you for your help again.
So when this is done. It is referencing a function (new) from cafe.pm?
anyway, I am not sure how to download the CPAN as i am looking the code from unix/linux environment.Code:my $temp = cafe->new(%cafe_switches);
This would assign to $temp whatever comes out of new(), usually a class instance in the form of a blessed hash reference. please read up on basic Perl and Perl OO.Code:cafe->new(%cafe_switches) # call "new" method in the cafe package, with argument %cafe_switches
If you're on Linux, you can just do (as root) on the commandline:anyway, I am not sure how to download the CPAN as i am looking the code from unix/linux environment.
If that doesn't work, tryCode:cpan install Data::Dumper
Best of luck.Code:perl -MCPAN -e 'install Data::Dumper'
I have one perl module call cafe.pm but I can print the message in some of its subroutine and some just return me the error message:
Can't use string ("cafe") as a HASH ref while "strict refs" in use
From the main code:
Both subroutine are under the same cafe.pm:my $temp2=cafe->Check();
my $temp1 = cafe->GetTestName();
This can successfully printed out when I run the main perl script:
But this return the error:Code:sub Check { print "Not yet implement \n"; }
And can somebody tell me if sub new() is a special subroutine in a package??? Coz I cant seems to print any message from it as well.Code:sub GetTestName() { print"GetTest\n"; my $self = shift; return $self->{_o}; }
For example when i try to see if it execute the sub new(), I put a print inside the subroutine:
But it doesnt print out the message when i do the:Code:sub new { my $class = shift; my (%switches) = @_; my ($self) = { }; bless $self, $class; foreach my $key (sort keys %switches) { $self->{$key} = $switches{$key}; print " in xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxst new\n"; }
Code:my $temp = cafe->new(%cafe_switches);
Nevermind about this post now.I have figured it out though.
If I have a code that will return $id= 0 and $id= 1 randomly but I would like to make it to get the $id=0 always first...how should I do it?
foreach my $link (sort @linker){
my $id = XMLP::Attribute($link,"ID");
$substr .= "_csi" ;
$substr .= $id ;
}
$style .= $substr;
and the end result should be always style_csi0_csi1 always...
![]()
You're being pretty sparse with your code, I cannot look in your head![]()
Based on this limited information I have no idea what's going on... If I were you, I'd check the workings (documentation) of XMLP::Attribute and see what it returns based on which arguments. Use the debugger.
Bookmarks