(il)logical - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

(il)logical

mercman2000mercman2000 Posts: 80Member
We started learning Perl yesterday in Unix class. Nothing fancy, just the basics. I'm semi-confused on it all.

For our first program, we counted the number of lines in a file called students, which was passed to the command line. It went like this:
$filein = $ARGV[0];

Or along those lines. Then, we had an open statement. Just fine. Here's where I'm getting confused at.
while (<>)
{
print $_;
++$line_count;
}

?? Maybe whiles here don't work the same as they do in basic or C++, but it looks to me like that statement doesn't have anything comparing that while to, so logically, the loop should continue. And, what's up with the _$?? Is that supposed to be some kind of 'get input from file' statement?

Next program wasn't too bad. A lot of the same stuff. We predefined the file's name this time.

The program after that was where things started to get hairy.
The program started with
[code]
#!/usr/bin/perl
I understand that.

went down, and
while (<>)
{
print _$;
++$line_count;
}
[/code]

Ok, that part doesn't bother me. That was about the sum of the program, but nowhere in there did I see an open statement, nor did I see a way to pull the variable off of the command line like I did with the first program. This is what confuses me. How, if I did not write the code to do so does the language know to pull the variable off of the command line, and know that the variable in question is a file and not something else, and how does it read and interact with the file without me telling it to? It's spooky. I hope this doesn't sound like mindless rambling to the vets of the language, but you can see my confusion coming from a language like C++ or similiar. You've got to tell it to do something, it doesn't do it unless it has the code to do it. If it does it automatically, I'll accept it with blind faith, much like I accept the fact any number raised to the 0th power is 1. I don't understand the logic, or the reason why, but since it's written in stone, I accept it. Even my teacher is confused. It's not that he doesn't understand it himself, it's just that no one has ever gotten this far in the Unix book, they all got hung up on the fact that "It's not windows.". So, we've gotten the farthest, and thus, the first time he's seen the problem. Some clarity, please?

Many Thanks.
Mercman2000

---------------------------------------------
I've got a plan, but I'm going to need a dead monkey, some empty liquor bottles, and a vacuum cleaner.

Comments

  • while(<>) {} in Perl is called the 'diamond operator' (stare closely at it to understand the name) and what it does is automatically open every file you give on the command line one by one, and read a line from them into $_, one by one.

    for example, if the program was set up with the diamond operator then 'perl script.pl file1 file2' would read file1 followed by file2 into $_, line by line. If you specifiy a file that doesnt exist then perl will say so and move onto the next one.

    And just so you know, you can think of $_ as perls default for everything, in most cases if you dont tell data somewhere else to go, it will go there, and if you dont say where to get data from, it will get it from there...note that this is in most cases, such as split() and print().
  • medina_raptormedina_raptor Posts: 6Member
    For files there is a lot of options, depends of how advanced are you or how efficient do you want the code.
    there is a variable in perl $. the have the input file number, so you can use and the you don't need to accumulate

    or in a easy way(not at all efficient) you can do
    open (FILE , "< file_to_read.txt");
    @array= ;
    close(FILE);
    $lines= scalar (@array);
    and the variables $lines will have the size of the array, and the size of the array is the number of lines in the file :)

    Easy, isn't it?

    Carlos

    : We started learning Perl yesterday in Unix class. Nothing fancy, just the basics. I'm semi-confused on it all.
    :
    : For our first program, we counted the number of lines in a file called students, which was passed to the command line. It went like this:
    : $filein = $ARGV[0];
    :
    : Or along those lines. Then, we had an open statement. Just fine. Here's where I'm getting confused at.
    : while (<>)
    : {
    : print $_;
    : ++$line_count;
    : }
    :
    : ?? Maybe whiles here don't work the same as they do in basic or C++, but it looks to me like that statement doesn't have anything comparing that while to, so logically, the loop should continue. And, what's up with the _$?? Is that supposed to be some kind of 'get input from file' statement?
    :
    : Next program wasn't too bad. A lot of the same stuff. We predefined the file's name this time.
    :
    : The program after that was where things started to get hairy.
    : The program started with
    : [code]
    : #!/usr/bin/perl
    : I understand that.
    :
    : went down, and
    : while (<>)
    : {
    : print _$;
    : ++$line_count;
    : }
    : [/code]
    :
    : Ok, that part doesn't bother me. That was about the sum of the program, but nowhere in there did I see an open statement, nor did I see a way to pull the variable off of the command line like I did with the first program. This is what confuses me. How, if I did not write the code to do so does the language know to pull the variable off of the command line, and know that the variable in question is a file and not something else, and how does it read and interact with the file without me telling it to? It's spooky. I hope this doesn't sound like mindless rambling to the vets of the language, but you can see my confusion coming from a language like C++ or similiar. You've got to tell it to do something, it doesn't do it unless it has the code to do it. If it does it automatically, I'll accept it with blind faith, much like I accept the fact any number raised to the 0th power is 1. I don't understand the logic, or the reason why, but since it's written in stone, I accept it. Even my teacher is confused. It's not that he doesn't understand it himself, it's just that no one has ever gotten this far in the Unix book, they all got hung up on the fact that "It's not windows.". So, we've gotten the farthest, and thus, the first time he's seen the problem. Some clarity, please?
    :
    : Many Thanks.
    : Mercman2000
    :
    : ---------------------------------------------
    : I've got a plan, but I'm going to need a dead monkey, some empty liquor bottles, and a vacuum cleaner.
    :



  • zedd386zedd386 Posts: 2Member
    The reason that anything raised to the 0 power = 1 is this:

    when you have a fraction like (x^3)/(x^1) you simplify by subtracting the exponents.

    x^(3 - 1) which is x^2.

    Now suppose that you have (x^3)/(x^3). you simplify by doing x^(3 - 3)
    which is x^0. Why is this 1?

    Anything over itself in a fraction is 1, like 2/2 or 7.5/7.5

    Hence x^0 is 1.

  • mercman2000mercman2000 Posts: 80Member
    lol, thats too funny you answered that. Thanks for clearing that up!
    ---------------------------------------------
    I've got a plan, but I'm going to need a dead monkey, some empty liquor bottles, and a vacuum cleaner.

  • mercman2000mercman2000 Posts: 80Member
    Thanks to both of you. I'll finally be able to look at code with a little clarity now.

    ---------------------------------------------
    I've got a plan, but I'm going to need a dead monkey, some empty liquor bottles, and a vacuum cleaner.

Sign In or Register to comment.