Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

I'm reading a book! It's given me more questons!

by Petras (Friar)
on Jul 28, 2005 at 10:17 UTC ( #478901=perlquestion: print w/ replies, xml ) Need Help??
Petras has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Everyone!

In my, "I don't want to be a newbie anymore!" quest I've been going through the Llama book. It's taught me a lot! I understand what /^\\[d-m_](\w)*\s/i means now. That's a big change. I also understand that I'd probably never need *that* search....

Wasted time aside, there were a few things I ran into that don't do what I thought they should do. Can someone tell me why?

This first question has to do with code trying to write (1..9).0 seven times in a row; making a column heading, so to speak. This first bit doesn't work:
my @step=(1..9); push (@step, 0); my $step; foreach (@step) { $step . $_; } print ($step x 7);
but this does:
my @step=(1..9); push (@step, 0); my @stepCount=(1..7); foreach (@stepCount) { print @step; }
yet they look like they should do the same thing. Why don't they?

The next bit of Why_Doesn't_it_do_It code is supposed to check for one of two strings, and make sure that it prints three times. Merlyn will probably recognize some of the arguments:
my $what = "fred|barney"; while (<>) { chomp; if ($_ =~ ($what){3}) { print "It's in there.\n"; } else { print "Not there.\n"; } }
doesn't do what this does:
my $what = "(fred|barney){3}"; while (<>) { chomp; if ($_ =~ $what) { print "It's in there.\n"; } else { print "Not there.\n"; } }

Yet to my don'tBeANewbieAnymore eyes it looks like the first code is basically the same as the second, if = means "=". Can someone tell me what's the real difference between the two bits of code?

Thanks. Cheers!
-p
Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.

-Howard Aiken

Comment on I'm reading a book! It's given me more questons!
Select or Download Code
Re: I'm reading a book! It's given me more questons!
by lestrrat (Deacon) on Jul 28, 2005 at 10:31 UTC
    my @step=(1..9); push (@step, 0); my $step; foreach (@step) { $step . $_; <<<<< HERE. This should be "$step .= $_;" print ($step x 7);

    The second example doesn't even compile. I could just show you the problem, but it would probably be more helpful for you to use strict; and figure it out ;)

Re: I'm reading a book! It's given me more questons!
by xdg (Monsignor) on Jul 28, 2005 at 10:49 UTC

    First of all, always run your code examples under "use strict" and "use warnings" -- they'll almost always give you hints when you don't get what you expect

    On your first question, the first foreach is using "." (concatenate) instead of an assignment.

    foreach (@step) { $step = $step . $_; # or shorthand: $step .= $_ }

    If you ran your code with warnings, you'd get this message:

    Use of uninitialized value in concatenation (.) or string

    On your second question, it's a little garbled as to what you're trying to do. For one, you're not using regular expression delimiters:

    $_ =~ /($what){3}/ # match things in $what 3 times

    Second, I'm not sure what you mean by "print three times", but the regex /(fred|barney){3}/ means to match any combination of "fred" or "barney" that appears three times in a row, e.g. "fredfredfred" or "fredbarneyfred" or any combination like that. I'm not sure that's what you intended.

    Hope that helps

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: I'm reading a book! It's given me more questons!
by polettix (Vicar) on Jul 28, 2005 at 13:48 UTC
    Others pointed out the problems with your code chunks, but I'd like to give some humble suggestions.

    I understand that it is proof-of-concept code, but the first example is probably better rephreased as:

    my @step = (1..9, 0); # Use of comma operator, no need for push my $step = join '', @step; # Use of specific function for the task print ($step x 7);
    and, of course, you don't even need a variable for the array:
    my $step = join '', (1..9, 0); print ($step x 7);
    or for the scalar:
    print ((1..9, 0) x 7);
    This results in less code and improved readability IMHO. Well, maybe the last is not so readable :)

    Regarding the second example, I suggest that you provide also some examples of how your scripts behaves, showing sample input, the resulting output and pointing out where you expected different results.

    Update: added one-line example and comment about readability.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: I'm reading a book! It's given me more questons!
by Codon (Friar) on Jul 28, 2005 at 22:27 UTC
    This won't compile:
    my $what = "fred|barney"; ... if ($_ =~ ($what){3}) { ... }
    Dropping the () around $what will make it compile, but that really changes what the code is saying.
    if ($_ =~ $what{3}) { ... }
    is going to try to find the value in %what that is assosciated with the key '3' and use that as a regex. Unless you have some %what hash, this will be undef and will be intrepreted as
    if ($_ =~ //) { ... }
    which will match anything, including nothing. Essentially, that's just
    if (1) { ... }
    and you most likely do not want that.

    This

    my $what = "(fred|barney){3}"; ... if ($_ =~ $what) { ... }
    probably does the closest to what you want (match three occurrences of 'fred' or 'barney' in any order. This works because =~ treats whatever it finds on the right hand side as a RE (regular expression).

    You could get the same behavior from either of your examples by using the m// operator (the 'm' can be omited if you are using slashes as the delimiter).

    if ($_ =~ /($what){3}/) { ... } .... if ($_ =~ /$what/) { ... }
    If you do this, then you can eliminate the '$_ =~' as it is redundant:
    if (/($what){3}/) { ... } ... if (/$what/) { ... }

    Ivan Heffner
    Sr. Software Engineer, DAS Lead
    WhitePages.com, Inc.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://478901]
Approved by Corion
Front-paged by kwaping
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2014-07-10 03:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (198 votes), past polls