Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Build your array with push

by Anonymous Monk
on Jan 31, 2006 at 16:37 UTC ( [id://526788]=note: print w/replies, xml ) Need Help??


in reply to Build your array with push

Yeah, but what if you've misspelled a name? Shouldn't it be easy to fix those kinds of mistakes? That's why I'd recommend formatting like...
my @folk = (); push (@folk, "H". "i". "b". "b". "s" ); push (@folk, "D". "a". "g". "l". "i". "s". "h" );
...etc. Of course that's also problematic if you want to edit an individual letter. So I find the style below a bit better (especially when using a modern editor like edlin). Plus it uses a more descriptive name than the bland "@folk"...
push (@Hibbs_Daglish_Schwartz_Vroom, chr(0b1001000). chr(0b1101001). chr(0b1100010). chr(0b1100010). chr(0b1110011) ); push (@Hibbs_Daglish_Schwartz_Vroom, chr(0b1000100). chr(0b1100001). chr(0b1100111). chr(0b1101100). chr(0b1101001). chr(0b1110011). chr(0b1101000) ); push (@Hibbs_Daglish_Schwartz_Vroom, chr(0b1010011). chr(0b1100011). chr(0b1101000). chr(0b1110111). chr(0b1100001). chr(0b1110010). chr(0b1110100). chr(0b1111010) ); push (@Hibbs_Daglish_Schwartz_Vroom, chr(0b1010110). chr(0b1110010). chr(0b1101111). chr(0b1101111). chr(0b1101101) );

Replies are listed 'Best First'.
Re^2: Build your array with push
by Anonymous Monk on Jan 31, 2006 at 16:49 UTC
    Of course, if you want to get rid of all that pesky horizontal scrolling you could pass something like the following through cpp...
    p\ u\ s\ h\ (\ @\ H\ i\ b\ b\ s\ _\ D\ a\ g\ l\ i\ s\ h\ _\ S\ c\ h\ w\ a\ r\ t\ z\ _\ V\ r\ o\ o\ m\ ,\ c\ h\ r\ (\ 0\ b\ 1\ 0\ 0\ 1\ 0\ 0\ 0\ )\ )\ ;\

      That won't work. Perl is not C.

        Sure it will, but you need the -P command-line switch (which is not recommended), cpp and sed.
        $ perl -P -MO=Deparse cpp.pl <stdin>:1:2: warning: backslash-newline at end of file push @Hibbs_Daglish_Schwartz_Vroom, 'H'; cpp.pl syntax OK
        That won't work. Perl is not C.
        Perl is pretty close though. What part of Perl's syntax makes CPP choke (just curious)?
Re^2: Build your array with push
by Corion (Patriarch) on Feb 01, 2006 at 10:25 UTC

    That style of code is totally ugly and unusable. What if your original list of data erroneously switched two characters and you now have to swap the last and next-to-last character? This coding style is flawed in that aspect. You should use the following coding style instead:

    push (@Hibbs_Daglish_Schwartz_Vroom, "" . chr(0b1010110) . chr(0b1110010) . chr(0b1101111) . chr(0b1101111) . chr(0b1101101) );

    Actually I use this coding style when manipulating SQL SELECT statements:

    SELECT monkey, bananas from my_table where 1 = 1 and flung_poo is null and size > 800 ;
      That style of code is totally ugly and unusable [...] You should use the following coding style instead:

      No, no, no! Yours is better than the previous alternatives but one may still get confused when swapping bits. And all those calls to chr, oh my! Now pack can do all this for cheap. Thus you should use the following coding style instead:

      push @Hibbs_Daglish_Schwartz_Vroom, pack qw/(B8)* 01010110 01110010 01101111 01101111 01101101 /;

      Except that one would almost certainly want to write a specialized sub to do this, should he later change his mind with respect to the order in which to store bits into chars, chars into strings and strings into the array:

      sub insert (\@@) { my $arr=shift; push @$arr, pack '(B8)*', @_; } # ... insert @Hibbs_Daglish_Schwartz_Vroom, qw/ 01010110 01110010 01101111 01101111 01101101 /;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-19 05:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found