The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl newbie . &&..programming newbie (question 2) xytiz Shell Programming and Scripting 1 05-07-2009 01:26 PM
Questions of a newbie Brycemb16 Linux 2 08-03-2005 03:13 PM
Newbie Questions mharley Shell Programming and Scripting 2 03-01-2005 10:08 AM
A few newbie questions hellz UNIX for Dummies Questions & Answers 7 09-01-2001 11:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-07-2009
byte1918 byte1918 is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 4
Perl newbie questions!

Hi,

So I started to learn perl a few days ago, and I have some problems...

One of my problems...
PHP Code:
#!C:\Perl64\bin\perl.exe -w
use LWP::Simple;
print 
"Content-Type: Text/Plain\n\n";

sub pagelinks {
return @
all get($_[0]) =~ /hrefs*=s*"?([^"s>]+)/gis;
}

@
pagelinks("http://website.com");

foreach 
$key (@a){
    print 
$key."\n";
        
        

so I tried using some regex I usualy have used in php with preg_match but they don't seem to work. So I got the above regex after searching google..
and tbh I don't understand it..
What I understand from it : href followed by none or more whitespaces followed by = followed by none or more whitespaces followed by 0 or 1 " and afterwards... I don';t get it at all! it's really frustrating.

my second problem..

let's assume I have this array :
@horse = ("mdamdamad" , "asdasda" ,"asdasd" ,"a");
I want to delete the element which is equal to "a".

foreach $key (@horse){
if ($key == "a"){
delete $horse[$key];}
}

the above code doesn't seem to work.. it deletes the @horse[-2] value. I have also tryied using $key =~ /a/ and (grep $key eq "a",@horse). All have failed me.. any suggestions? please...

Last edited by byte1918; 06-07-2009 at 01:55 PM..
  #2 (permalink)  
Old 06-07-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
When you post perl code use the perl highlighter, not the php highlighter which can mangle some perl code.

Your shortcut character class in the regexp are wrong, the shortcut classes like a space need a backslash \s:


Code:
return @all = get($_[0]) =~ /href\s*=\s*"?([^\s>]+)/gis;

I don't know if that will make your code do anything useful but it at least corrects the mistake in the regex.
  #3 (permalink)  
Old 06-07-2009
byte1918 byte1918 is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 4
Quote:
Originally Posted by KevinADC View Post
When you post perl code use the perl highlighter, not the php highlighter which can mangle some perl code.

Your shortcut character class in the regexp are wrong, the shortcut classes like a space need a backslash \s:


Code:
return @all = get($_[0]) =~ /href\s*=\s*"?([^\s>]+)/gis;

I don't know if that will make your code do anything useful but it at least corrects the mistake in the regex.
Sry about using perl code in the php code tag must of missed the <code> tag.

Anyway I know about escaping the metacharacters, and I did that, but dunno for what reason I copied it wrong, not sure. thx for pointing that out though :P

One more question :
Afaik in a sub $_[0],$_[1] refers to the first and second parameter which was sent to the function (correct me if im wrong please). So for example I have
Code:
#!C:\Perl64\bin\perl.exe -w
print "Content-Type: Text-plain\n\n";

@horse = ("mdamdamad" , "asdasda" ,"asdasd" ,"a");

getindex ("asd",@horse);

sub getindex {
$string = $_[0];
@a = $_[1];
print $string."\n";
foreach $key (@a){
    print $key."\n";
}
}

the problem is that @a in getindex only gets the first value of @horse , and not the entire array. How do I manage to pass to it the whole array?
edit: i found out the answer I had to use @a= @_;

Last edited by byte1918; 06-07-2009 at 04:48 PM..
  #4 (permalink)  
Old 06-07-2009
durden_tyler's Avatar
durden_tyler durden_tyler is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 548
Quote:
Originally Posted by byte1918 View Post
...
I want to delete the element which is equal to "a".
...

Code:
$
$ perl -e '{@horse = ("mdamdamad", "asdasda", "asdasd", "a"); foreach $i (@horse) {pop @horse,$i if $i eq "a"} print "@horse"}'
mdamdamad asdasda asdasd$
$

tyler_durden
  #5 (permalink)  
Old 06-07-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731

Code:
#!C:\Perl64\bin\perl.exe -w
print "Content-Type: Text/plain\n\n";#<-- this is not necessary unless running as a CGI script

@horse = ("mdamdamad" , "asdasda" ,"asdasd" ,"a");

getindex ("asd",@horse);

sub getindex {
   ($string,@a) = @_;
   print $string."\n";
   foreach $key (@a){
      print $key."\n";
   }
}

You're going to want to learn how to use references in perl really quickly insterad of trying to pass mixed data types like int he code above. An example:


Code:
#!C:\Perl64\bin\perl.exe -w
print "Content-Type: Text/plain\n\n";

@horse = ("mdamdamad" , "asdasda" ,"asdasd" ,"a");

getindex ("asd",\@horse);

sub getindex {
   my ($string,$array_ref) = @_;
   print $string."\n";
   foreach $key (@{$array_ref}){
      print $key."\n";
   }
}

The first three tutorials on this page discuss references:

Tutorials - perldoc.perl.org

Also:

you will want to use "warnings" and "strict" pragmas and start declaring your variables properly when writing perl code.


Code:
#!C:\Perl64\bin\perl.exe
use warnings;
use strict;
print "Content-Type: Text/plain\n\n";

my @horse = ("mdamdamad" , "asdasda" ,"asdasd" ,"a");

getindex ("asd",\@horse);

sub getindex {
   my ($string, $array_ref) = @_;
   print $string."\n";
   foreach $key (@{$array_ref}){
      print $key."\n";
   }
}

Also, don't use $a and $b as private variables in your perl scripts. Perl uses them for sorting data.

The http header was also wrong, you had: Text-plain should be: Text/plain
  #6 (permalink)  
Old 06-07-2009
byte1918 byte1918 is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 4
Quote:
Originally Posted by KevinADC View Post
Code:
#!C:\Perl64\bin\perl.exe -w
print "Content-Type: Text/plain\n\n";#<-- this is not necessary unless running as a CGI script

@horse = ("mdamdamad" , "asdasda" ,"asdasd" ,"a");

getindex ("asd",@horse);

sub getindex {
   ($string,@a) = @_;
   print $string."\n";
   foreach $key (@a){
      print $key."\n";
   }
}

You're going to want to learn how to use references in perl really quickly insterad of trying to pass mixed data types like int he code above. An example:


Code:
#!C:\Perl64\bin\perl.exe -w
print "Content-Type: Text/plain\n\n";

@horse = ("mdamdamad" , "asdasda" ,"asdasd" ,"a");

getindex ("asd",\@horse);

sub getindex {
   my ($string,$array_ref) = @_;
   print $string."\n";
   foreach $key (@{$array_ref}){
      print $key."\n";
   }
}

The first three tutorials on this page discuss references:

Tutorials - perldoc.perl.org

Also:

you will want to use "warnings" and "strict" pragmas and start declaring your variables properly when writing perl code.


Code:
#!C:\Perl64\bin\perl.exe
use warnings;
use strict;
print "Content-Type: Text/plain\n\n";

my @horse = ("mdamdamad" , "asdasda" ,"asdasd" ,"a");

getindex ("asd",\@horse);

sub getindex {
   my ($string, $array_ref) = @_;
   print $string."\n";
   foreach $key (@{$array_ref}){
      print $key."\n";
   }
}

Also, don't use $a and $b as private variables in your perl scripts. Perl uses them for sorting data.

The http header was also wrong, you had: Text-plain should be: Text/plain
thanks alot KevinADC your response was very helpful!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0