Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^6: Perl vs C

by JavaFan (Canon)
on Mar 16, 2009 at 14:07 UTC ( [id://750900]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Perl vs C
in thread Perl vs C

I don't understand this comment: "lists are unmutable". Please explain, I just don't understand what you mean.
Try to make pure Perl program that modifies a list. Come back when you either have such a program, or when you are convince this isn't possible.

Replies are listed 'Best First'.
Re^7: Perl vs C
by Marshall (Canon) on Mar 16, 2009 at 14:15 UTC
    Here is a simple thing that removes things from a Perl list that do not match a criteria.
    #!usr/bin/perl -w use warnings; my @list = ("a1", "b23", "c45", "d1", "b43", "b65"); print join (" ",@list),"\n"; #prints: a1 b23 c45 d1 b43 b65 @list = grep{!/^b/}@list; #remove things that start with b print join (" ",@list),"\n"; #prints: a1 c45 d1
      Now you're modifying an array, swapping one value (a list) for another (another list). Calling the array @list doesn't change the fact it's an array. The code below doesn't modify '3' either - it just replaces the value of a variable.
      my $var = 3; $var++; say $var; # Prints 4.
        I don't understand what you mean. Sure it is possible to modify a list! Just like in C, I can pass a reference to a list to be modified and it will be modified! In the code below, I am modifying the actual list since the sub uses the reference to that list.
        #!usr/bin/perl -w use warnings; sub modify_list { my $listRef = shift; @$listRef = grep{!/^b/}@$listRef; #note there is no "return" value here #the list has been modified!!!! } my @list = ("a1", "b23", "c45", "d1", "b43", "b65"); print join (" ",@list),"\n"; #prints: a1 b23 c45 d1 b43 b65 modify_list(\@list); print join (" ",@list),"\n"; #prints a1 c45 d1

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://750900]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2026-02-18 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.