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

Perl list emulation in Java...

by rje (Deacon)
on Jan 09, 2002 at 02:42 UTC ( [id://137284]=CUFP: print w/replies, xml ) Need Help??

I don't know where to put this code, because it's a Java emulation of some of the perl list's common functions. Even though it's Java, I thought folks might be interested and/or amused to see where my whimsy takes me.

//package gen.eaglestone.util; import java.util.*; /***************************************************************** * * PerlList roughly imitates the perl list type, providing * the following methods: * * push( Object scalar ) * push( Object[] array ) * push( PerlList list ) * * Object pop() * * unshift( Object scalar ) * unshift( Object[] array ) * unshift( PerlList list ) * * Object shift() * * # PerlList split( String delims, String scalar ) * * String join( String joiner ) * # String join( String joiner, PerlList[] lists ) * * Object $( int index ) *****************************************************************/
public class PerlList extends Vector { /************************************************************* * * constructors * *************************************************************/ public PerlList() { super(); } public PerlList( int cap ) { super( cap ); } /************************************************************* * * push() and pop() * *************************************************************/ public void push( PerlList list ) { Enumeration elems = list.elements(); while( elems.hasMoreElements() ) addElement( elems.nextElement() ); } public void push( Object[] elems ) { for( int i=0; i<elems.length; i++ ) addElement( elems[i] ); } public void push( Object scalar ) { addElement( scalar ); } public Object pop() { Object scalar = lastElement(); removeElement( scalar ); return scalar; } /************************************************************* * * shift() and unshift() * *************************************************************/ public Object shift() { Object scalar = firstElement(); removeElement( scalar ); return scalar; } public void unshift( Object scalar ) { insertElementAt( scalar, 0 ); } public void unshift( Object[] elems ) { for( int i=0; i<elems.length; i++ ) insertElementAt( elems[i], 0 ); } public void unshift( PerlList list ) { Enumeration elems = list.elements(); while( elems.hasMoreElements() ) insertElementAt( elems.nextElement(), 0 ); } /************************************************************* * * split() and join() * *************************************************************/ public static PerlList split( String delims, String scalar ) { PerlList l = new PerlList(); StringTokenizer tok = new StringTokenizer( scalar, delims ); while( tok.hasMoreTokens() ) { l.push( tok.nextToken() ); } return l; } public String join( String joiner ) { StringBuffer buf = new StringBuffer(); Enumeration elems = elements(); if( elems.hasMoreElements() ) { buf.append( (String) elems.nextElement() ); } while( elems.hasMoreElements() ) { buf.append( joiner ); buf.append( (String) elems.nextElement() ); } return buf.toString(); } public static String join( String joiner, PerlList[] list ) { PerlList nu = new PerlList(); for( int i=0; i<list.length; i++ ) nu.push( list[i] ); return nu.join( joiner ); } /************************************************************* * * default access * *************************************************************/ public Object $( int index ) { return (String) elementAt( index ); } /************************************************************* * * test * *************************************************************/ public static void main( String args[] ) { String a = "abcd 123-4 b ag ag ag x 567"; PerlList pl = PerlList.split( " ", a ); System.out.println( pl.join( "\n" ) ); System.out.println( pl.$(0) ); } }

Replies are listed 'Best First'.
(Ovid) Re: Perl list emulation in Java...
by Ovid (Cardinal) on Jan 09, 2002 at 03:02 UTC

    Interestingly, this is not terribly uncommon. While living in Amsterdam and being forced to endure the hell that is VBScript, I wrote many of the same routines for that language. Like you, though, I failed to emulate them properly. For example, push actually returns the number of elements added to the array:

    public int push( PerlList list ) { Enumeration elems = list.elements(); while( elems.hasMoreElements() ) addElement( elems.nextElement() ); return list.size(); }

    Of course, I can't help but wonder if that's a waste of time. I don't recall ever seeing any code that actually checks that return value, but then, if you're going to emulate... :)

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

(RhetTbull) Re: Perl list emulation in Java...
by RhetTbull (Curate) on Jan 09, 2002 at 06:51 UTC
Re: Perl list emulation in Java...
by mdog (Pilgrim) on Jan 09, 2002 at 23:23 UTC
    Very Cool!

    Whenever I dabble in Java, I always wish that I could just use the functions I am used to...

    If you have any more Perl-esque Java, I'd be interested in seeing it.

    Lazy Mdog

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found