FAQ
Hi,

Suppose I have this code:

#/usr/local/bin/perl
use warnings;
use strict;
use subs 'verify';

my %where=(
Gary => "Dallas",
Lucy => "Exeter",
Ian => "Reading",
Samantha => "Oregon"
);

# Then i open a logfile

open FH, '<', $logfile or die "Can't open $logfile!: $!";

# Now, for every line in the logfile, I will retrive a particular string, let's say a person's name and verify if it exists in my hash above:

while (<FH>){
(my $person) = $_ =~ /person=(\S+)/;
my $personstatus = verify(\%where,$person);

# And do anything if the personstatus is this and that.


# Now, on my sub "verify", I would like to know if the person exists in my hash.

sub verify {
my $where = shift @_;
my $person = shift @_;
my $personstatus;

if (exists $$where{$person}){

$personstatus =1;
}else {
$personstatus = 0;
}

$personstatus;
}

Am I thinking as this is how it should be done when passing hashes to a subroutine? My program doesn't work and I know it has something to do with the "my $personstatus" line as well as "if (exists $$where{$person} line.
Can you show me the right way of passing hashes to subroutines? Thanks.

I also tried declaring %where as 'our' so that I can use "if (exists %where{$person}" instead of "if(exists $$where{person}) but i still can't make it work.


Do you know if I am making any sense here?

Thanks









____________________________________________________________________________________
Don't pick lemons.
See all the new 2007 cars at Yahoo! Autos.
http://autos.yahoo.com/new_cars.html

Search Discussions

  • Jeff Pang at Jan 18, 2007 at 7:17 am
    Hi,

    I've copied your codes and format them on my editor and it seems run well for me.

    use warnings;
    use strict;

    my %where=(
    Gary => "Dallas",
    Lucy => "Exeter",
    Ian => "Reading",
    Samantha => "Oregon"
    );
    my $logfile = 'foo.txt';

    open FH, '<', $logfile or die "Can't open $logfile!: $!";
    while (<FH>){
    my ($person) = /person=(\S+)/;
    my $personstatus = verify(\%where,$person);
    if ($personstatus) {
    print "$person exist\n";
    }
    }
    close FH;

    sub verify {
    my $where = shift @_;
    my $person = shift @_;
    my $personstatus;

    if (exists $where->{$person}){ # use '->' operator for hash's reference
    # if (exists $$where{$person}){ # this line also can work
    $personstatus =1;
    }else {
    $personstatus = 0;
    }

    $personstatus;
    }




    Jeff Pang
    2007-01-18



    发件人: Michael Alipio
    发送时间: 2007-01-18 14:26:00
    收件人: begginers perl.org
    抄送:
    主题: Passing hash into sub routines (should i use reference?)

    Hi,

    Suppose I have this code:

    #/usr/local/bin/perl
    use warnings;
    use strict;
    use subs 'verify';

    my %where=(
    Gary = > "Dallas",
    Lucy = > "Exeter",
    Ian = > "Reading",
    Samantha = > "Oregon"
    );

    # Then i open a logfile

    open FH, ' <', $logfile or die "Can't open $logfile!: $!";

    # Now, for every line in the logfile, I will retrive a particular string, let's say a person's name and verify if it exists in my hash above:

    while ( <FH >){
    (my $person) = $_ =~ /person=(\S+)/;
    my $personstatus = verify(\%where,$person);

    # And do anything if the personstatus is this and that.


    # Now, on my sub "verify", I would like to know if the person exists in my hash.

    sub verify {
    my $where = shift @_;
    my $person = shift @_;
    my $personstatus;

    if (exists $$where{$person}){

    $personstatus =1;
    }else {
    $personstatus = 0;
    }

    $personstatus;
    }

    Am I thinking as this is how it should be done when passing hashes to a subroutine? My program doesn't work and I know it has something to do with the "my $personstatus" line as well as "if (exists $$where{$person} line.
    Can you show me the right way of passing hashes to subroutines? Thanks.

    I also tried declaring %where as 'our' so that I can use "if (exists %where{$person}" instead of "if(exists $$where{person}) but i still can't make it work.


    Do you know if I am making any sense here?

    Thanks









    ____________________________________________________________________________________
    Don't pick lemons.
    See all the new 2007 cars at Yahoo! Autos.
    http://autos.yahoo.com/new_cars.html
  • Arun S at Jan 18, 2007 at 7:38 am
    Hi,

    Suppose I have this code:

    #/usr/local/bin/perl
    use warnings;
    use strict;
    use subs 'verify';

    my %where=(
    Gary => "Dallas",
    Lucy => "Exeter",
    Ian => "Reading",
    Samantha => "Oregon"
    );

    # Then i open a logfile

    open FH, '<', $logfile or die "Can't open $logfile!: $!";

    # Now, for every line in the logfile, I will retrive a particular string,
    let's say a person's name and verify if it exists in my hash above:

    while (<FH>){
    (my $person) = $_ =~ /person=(\S+)/;
    my $personstatus = verify(\%where,$person);

    # And do anything if the personstatus is this and that.


    # Now, on my sub "verify", I would like to know if the person exists in my
    hash.

    sub verify {
    my $where = shift @_;
    my $person = shift @_;
    my $personstatus;

    if (exists $$where{$person}){

    $personstatus =1;
    }else {
    $personstatus = 0;
    }

    $personstatus;
    }

    Am I thinking as this is how it should be done when passing hashes to a
    subroutine? My program doesn't work and I know it has something to do with
    the "my $personstatus" line as well as "if (exists $$where{$person} line.
    Can you show me the right way of passing hashes to subroutines? Thanks.

    I also tried declaring %where as 'our' so that I can use "if (exists
    %where{$person}" instead of "if(exists $$where{person}) but i still can't
    make it work.


    Do you know if I am making any sense here?

    Thanks
    Try the following code:
    while (<FH>)
    {
    (my $person) = $_ =~ /person=(\S+)/;
    }

    # i am passing just the $person
    if (($personstatus = verify($person)==1))
    {
    print "Found $person in hash";
    }
    else
    {
    print"sorry";
    }

    sub verify
    {
    (my $per) = @_;
    foreach $temp(sort keys %where)
    {
    if ($temp eq $per)
    {
    $found =1 ;
    goto xxx;
    }
    }
    xxx:
    return $found;
    }








    ____________________________________________________________________________________

    Don't pick lemons.
    See all the new 2007 cars at Yahoo! Autos.
    http://autos.yahoo.com/new_cars.html


    Whilst this email has been checked for all known viruses, recipients should undertake their own virus checking as Xansa will not accept any liability whatsoever.

    This email and any files transmitted with it are confidential and protected by client privilege. It is solely for the use of the intended recipient.
    Please delete it and notify the sender if you have received it in
    error. Unauthorised use is prohibited.

    Any opinions expressed in this email are those of the individual and not
    necessarily the organisation.
    Xansa, Registered Office: 420 Thames Valley Park Drive,
    Thames Valley Park, Reading, RG6 1PU, UK.
    Registered in England No.1000954.
    t +44 (0)8702 416181
    w www.xansa.com
  • Richard Jones at Jan 19, 2007 at 12:26 pm

    Michael Alipio wrote:
    Hi,

    Suppose I have this code:

    #/usr/local/bin/perl use warnings; use strict; use subs 'verify';

    my %where=( Gary => "Dallas", Lucy => "Exeter", Ian => "Reading",
    Samantha => "Oregon" );

    # Then i open a logfile

    open FH, '<', $logfile or die "Can't open $logfile!: $!";

    # Now, for every line in the logfile, I will retrive a particular
    string, let's say a person's name and verify if it exists in my hash
    above:

    while (<FH>){ (my $person) = $_ =~ /person=(\S+)/; my $personstatus =
    verify(\%where,$person);

    # And do anything if the personstatus is this and that.


    # Now, on my sub "verify", I would like to know if the person exists
    in my hash.

    sub verify { my $where = shift @_; my $person = shift @_; my
    $personstatus;

    if (exists $$where{$person}){

    $personstatus =1; }else { $personstatus = 0; }

    $personstatus; }

    Am I thinking as this is how it should be done when passing hashes to
    a subroutine? My program doesn't work and I know it has something to
    do with the "my $personstatus" line as well as "if (exists
    $$where{$person} line. Can you show me the right way of passing
    hashes to subroutines? Thanks.

    I also tried declaring %where as 'our' so that I can use "if (exists
    %where{$person}" instead of "if(exists $$where{person}) but i still
    can't make it work.


    Do you know if I am making any sense here?

    Thanks
    Why pass a reference to the same hash for every line of the <FH>? You
    have declared the %where hash at the top so it's already available to
    the sub-routine. Much simpler:

    while (<FH>) {
    my ($person) = $_ =~ /person=(\S+)/; # are you sure this works?
    my $personstatus = verify($person);
    }

    sub verify {
    local $_ = shift; # $_ contains $person from while loop

    return $where{$_} ? 1 : 0;
    }


    Or even simpler, dispose of verify() and just do:

    while (<FH>){
    my ($person) = $_ =~ /person=(\S+)/;
    my $personstatus = $where{$person} ? 1 : 0;
    }

    The correct way to accept a hashref in a sub-routine is:

    sub my_subroutine {
    my $hashref = shift;
    my $something_else = shift;

    # OR:
    my ($hashref, $something_else) = @_;
    }
    --
    ra(dot)jones(at)dpw(dot)clara(dot)co(dot)uk

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJan 18, '07 at 6:24a
activeJan 19, '07 at 12:26p
posts4
users4
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase